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.
39 #include "sim/host.hh"
40 #include "base/cprintf.hh"
41 #include "base/misc.hh"
42 #include "cpu/full_cpu/smt.hh"
44 #include "sim/annotation.hh"
45 #include "cpu/exec_context.hh"
46 #include "cpu/base_cpu.hh"
47 #include "sim/debug.hh"
48 #include "cpu/simple_cpu/simple_cpu.hh"
49 #include "base/inifile.hh"
50 #include "mem/mem_interface.hh"
51 #include "mem/base_mem.hh"
52 #include "cpu/static_inst.hh"
55 #include "mem/functional_mem/memory_control.hh"
56 #include "mem/functional_mem/physical_memory.hh"
57 #include "targetarch/alpha_memory.hh"
58 #include "sim/system.hh"
60 #include "mem/functional_mem/functional_memory.hh"
61 #include "sim/prog.hh"
65 #include "cpu/exetrace.hh"
66 #include "base/trace.hh"
67 #include "sim/sim_events.hh"
68 #include "base/pollevent.hh"
69 #include "sim/sim_object.hh"
70 #include "sim/sim_stats.hh"
72 #include "base/range.hh"
73 #include "base/loader/symtab.hh"
76 #include "targetarch/vtophys.hh"
77 #include "dev/pciareg.h"
78 #include "base/remote_gdb.hh"
79 #include "dev/alpha_access.h"
85 SimpleCPU::CacheCompletionEvent::CacheCompletionEvent(SimpleCPU
*_cpu
)
86 : Event(&mainEventQueue
),
91 void SimpleCPU::CacheCompletionEvent::process()
93 cpu
->processCacheCompletion();
97 SimpleCPU::CacheCompletionEvent::description()
99 return "cache completion event";
103 SimpleCPU::SimpleCPU(const string
&_name
,
105 Counter max_insts_any_thread
,
106 Counter max_insts_all_threads
,
107 Counter max_loads_any_thread
,
108 Counter max_loads_all_threads
,
109 AlphaItb
*itb
, AlphaDtb
*dtb
,
110 FunctionalMemory
*mem
,
111 MemInterface
*icache_interface
,
112 MemInterface
*dcache_interface
,
113 int cpu_id
, Tick freq
)
114 : BaseCPU(_name
, /* number_of_threads */ 1,
115 max_insts_any_thread
, max_insts_all_threads
,
116 max_loads_any_thread
, max_loads_all_threads
,
117 _system
, cpu_id
, freq
),
119 SimpleCPU::SimpleCPU(const string
&_name
, Process
*_process
,
120 Counter max_insts_any_thread
,
121 Counter max_insts_all_threads
,
122 Counter max_loads_any_thread
,
123 Counter max_loads_all_threads
,
124 MemInterface
*icache_interface
,
125 MemInterface
*dcache_interface
)
126 : BaseCPU(_name
, /* number_of_threads */ 1,
127 max_insts_any_thread
, max_insts_all_threads
,
128 max_loads_any_thread
, max_loads_all_threads
),
130 tickEvent(this), xc(NULL
), cacheCompletionEvent(this)
133 xc
= new ExecContext(this, 0, system
, itb
, dtb
, mem
, cpu_id
);
138 xc
->setStatus(ExecContext::Unallocated
);
140 //Open a GDB debug session on port (7000 + the cpu_id)
141 (new GDBListener(new RemoteGDB(system
, xc
), 7000 + cpu_id
))->listen();
143 AlphaISA::init(system
->physmem
, &xc
->regs
);
147 IntReg
*ipr
= xc
->regs
.ipr
;
148 ipr
[TheISA::IPR_MCSR
] = 0x6;
150 AlphaISA::swap_palshadow(&xc
->regs
, true);
153 ipr
[TheISA::IPR_PAL_BASE
] + AlphaISA::fault_addr
[fault
];
154 xc
->regs
.npc
= xc
->regs
.pc
+ sizeof(MachInst
);
163 AlphaISA::init(system
->physmem
, &xc
->regs
);
167 IntReg
*ipr
= xc
->regs
.ipr
;
168 ipr
[TheISA::IPR_MCSR
] = 0x6;
170 AlphaISA::swap_palshadow(&xc
->regs
, true);
172 xc
->regs
.pc
= ipr
[TheISA::IPR_PAL_BASE
] + AlphaISA::fault_addr
[fault
];
173 xc
->regs
.npc
= xc
->regs
.pc
+ sizeof(MachInst
);
176 tickEvent
.schedule(0);
180 xc
= new ExecContext(this, /* thread_num */ 0, _process
, /* asid */ 0);
182 if (xc
->status() == ExecContext::Active
) {
184 tickEvent
.schedule(0);
187 #endif // !FULL_SYSTEM
189 icacheInterface
= icache_interface
;
190 dcacheInterface
= dcache_interface
;
192 memReq
= new MemReq();
195 memReq
->data
= new uint8_t[64];
203 contexts
.push_back(xc
);
206 SimpleCPU::~SimpleCPU()
211 SimpleCPU::regStats()
216 .name(name() + ".num_insts")
217 .desc("Number of instructions executed")
221 .name(name() + ".num_refs")
222 .desc("Number of memory references")
226 .name(name() + ".idle_cycles")
227 .desc("Number of idle cycles")
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 idleFraction
= idleCycles
/ simTicks
;
249 numInsts
= Statistics::scalar(numInst
);
250 simInsts
+= numInsts
;
254 SimpleCPU::serialize()
260 // do we need this anymore?? egh
261 childOut("itb", xc
->itb
);
262 childOut("dtb", xc
->dtb
);
263 childOut("physmem", physmem
);
267 for (int i
= 0; i
< NumIntRegs
; i
++) {
269 ccprintf(buf
, "R%02d", i
);
270 paramOut(buf
.str(), xc
->regs
.intRegFile
[i
]);
272 for (int i
= 0; i
< NumFloatRegs
; i
++) {
274 ccprintf(buf
, "F%02d", i
);
275 paramOut(buf
.str(), xc
->regs
.floatRegFile
.d
[i
]);
277 // CPUTraitsType::serializeSpecialRegs(getProxy(), xc->regs);
281 SimpleCPU::unserialize(IniFile
&db
, const string
&category
, ConfigNode
*node
)
285 for (int i
= 0; i
< NumIntRegs
; i
++) {
287 ccprintf(buf
, "R%02d", i
);
288 db
.findDefault(category
, buf
.str(), data
);
289 to_number(data
,xc
->regs
.intRegFile
[i
]);
291 for (int i
= 0; i
< NumFloatRegs
; i
++) {
293 ccprintf(buf
, "F%02d", i
);
294 db
.findDefault(category
, buf
.str(), data
);
295 xc
->regs
.floatRegFile
.d
[i
] = strtod(data
.c_str(),NULL
);
298 // Read in Special registers
300 // CPUTraitsType::unserializeSpecialRegs(db,category,node,xc->regs);
304 change_thread_state(int thread_number
, int activate
, int priority
)
308 // precise architected memory state accessor macros
311 SimpleCPU::read(Addr addr
, T
& data
, unsigned flags
)
313 memReq
->reset(addr
, sizeof(T
), flags
);
315 // translate to physical address
316 Fault fault
= xc
->translateDataReadReq(memReq
);
318 // do functional access
319 if (fault
== No_Fault
)
320 fault
= xc
->read(memReq
, data
);
323 traceData
->setAddr(addr
);
324 if (fault
== No_Fault
)
325 traceData
->setData(data
);
328 // if we have a cache, do cache access too
329 if (fault
== No_Fault
&& dcacheInterface
) {
331 memReq
->completionEvent
= NULL
;
332 memReq
->time
= curTick
;
333 memReq
->flags
&= ~UNCACHEABLE
;
334 MemAccessResult result
= dcacheInterface
->access(memReq
);
336 // Ugly hack to get an event scheduled *only* if the access is
337 // a miss. We really should add first-class support for this
339 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
340 memReq
->completionEvent
= &cacheCompletionEvent
;
341 setStatus(DcacheMissStall
);
348 #ifndef DOXYGEN_SHOULD_SKIP_THIS
352 SimpleCPU::read(Addr addr
, uint64_t& data
, unsigned flags
);
356 SimpleCPU::read(Addr addr
, uint32_t& data
, unsigned flags
);
360 SimpleCPU::read(Addr addr
, uint16_t& data
, unsigned flags
);
364 SimpleCPU::read(Addr addr
, uint8_t& data
, unsigned flags
);
366 #endif //DOXYGEN_SHOULD_SKIP_THIS
370 SimpleCPU::read(Addr addr
, double& data
, unsigned flags
)
372 return read(addr
, *(uint64_t*)&data
, flags
);
377 SimpleCPU::read(Addr addr
, float& data
, unsigned flags
)
379 return read(addr
, *(uint32_t*)&data
, flags
);
385 SimpleCPU::read(Addr addr
, int32_t& data
, unsigned flags
)
387 return read(addr
, (uint32_t&)data
, flags
);
393 SimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
396 traceData
->setAddr(addr
);
397 traceData
->setData(data
);
400 memReq
->reset(addr
, sizeof(T
), flags
);
402 // translate to physical address
403 Fault fault
= xc
->translateDataWriteReq(memReq
);
405 // do functional access
406 if (fault
== No_Fault
)
407 fault
= xc
->write(memReq
, data
);
409 if (fault
== No_Fault
&& dcacheInterface
) {
411 memcpy(memReq
->data
,(uint8_t *)&data
,memReq
->size
);
412 memReq
->completionEvent
= NULL
;
413 memReq
->time
= curTick
;
414 memReq
->flags
&= ~UNCACHEABLE
;
415 MemAccessResult result
= dcacheInterface
->access(memReq
);
417 // Ugly hack to get an event scheduled *only* if the access is
418 // a miss. We really should add first-class support for this
420 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
421 memReq
->completionEvent
= &cacheCompletionEvent
;
422 setStatus(DcacheMissStall
);
426 if (res
&& (fault
== No_Fault
))
427 *res
= memReq
->result
;
433 #ifndef DOXYGEN_SHOULD_SKIP_THIS
436 SimpleCPU::write(uint64_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
440 SimpleCPU::write(uint32_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
444 SimpleCPU::write(uint16_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
448 SimpleCPU::write(uint8_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
450 #endif //DOXYGEN_SHOULD_SKIP_THIS
454 SimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
456 return write(*(uint64_t*)&data
, addr
, flags
, res
);
461 SimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
463 return write(*(uint32_t*)&data
, addr
, flags
, res
);
469 SimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
471 return write((uint32_t)data
, addr
, flags
, res
);
477 SimpleCPU::dbg_vtophys(Addr addr
)
479 return vtophys(xc
, addr
);
481 #endif // FULL_SYSTEM
487 SimpleCPU::processCacheCompletion()
490 case IcacheMissStall
:
491 icacheStallCycles
+= curTick
- lastIcacheStall
;
492 setStatus(IcacheMissComplete
);
494 case DcacheMissStall
:
495 dcacheStallCycles
+= curTick
- lastDcacheStall
;
499 panic("SimpleCPU::processCacheCompletion: bad state");
506 SimpleCPU::post_interrupt(int int_num
, int index
)
508 BaseCPU::post_interrupt(int_num
, index
);
510 if (xc
->status() == ExecContext::Suspended
) {
511 DPRINTF(IPI
,"Suspended Processor awoke\n");
512 xc
->setStatus(ExecContext::Active
);
513 Annotate::Resume(xc
);
516 #endif // FULL_SYSTEM
518 /* start simulation, program loaded, processor precise state initialized */
525 if (fault
== No_Fault
&& AlphaISA::check_interrupts
&&
526 xc
->cpu
->check_interrupts() &&
527 !PC_PAL(xc
->regs
.pc
) &&
528 status() != IcacheMissComplete
) {
531 AlphaISA::check_interrupts
= 0;
532 IntReg
*ipr
= xc
->regs
.ipr
;
534 if (xc
->regs
.ipr
[TheISA::IPR_SIRR
]) {
535 for (int i
= TheISA::INTLEVEL_SOFTWARE_MIN
;
536 i
< TheISA::INTLEVEL_SOFTWARE_MAX
; i
++) {
537 if (ipr
[TheISA::IPR_SIRR
] & (ULL(1) << i
)) {
538 // See table 4-19 of 21164 hardware reference
539 ipl
= (i
- TheISA::INTLEVEL_SOFTWARE_MIN
) + 1;
540 summary
|= (ULL(1) << i
);
545 uint64_t interrupts
= xc
->cpu
->intr_status();
546 for (int i
= TheISA::INTLEVEL_EXTERNAL_MIN
;
547 i
< TheISA::INTLEVEL_EXTERNAL_MAX
; i
++) {
548 if (interrupts
& (ULL(1) << i
)) {
549 // See table 4-19 of 21164 hardware reference
551 summary
|= (ULL(1) << i
);
555 if (ipr
[TheISA::IPR_ASTRR
])
556 panic("asynchronous traps not implemented\n");
558 if (ipl
&& ipl
> xc
->regs
.ipr
[TheISA::IPR_IPLR
]) {
559 ipr
[TheISA::IPR_ISR
] = summary
;
560 ipr
[TheISA::IPR_INTID
] = ipl
;
561 xc
->ev5_trap(Interrupt_Fault
);
563 DPRINTF(Flow
, "Interrupt! IPLR=%d ipl=%d summary=%x\n",
564 ipr
[TheISA::IPR_IPLR
], ipl
, summary
);
569 // maintain $r0 semantics
570 xc
->regs
.intRegFile
[ZeroReg
] = 0;
572 xc
->regs
.floatRegFile
.d
[ZeroReg
] = 0.0;
573 #endif // TARGET_ALPHA
575 if (status() == IcacheMissComplete
) {
576 // We've already fetched an instruction and were stalled on an
577 // I-cache miss. No need to fetch it again.
582 // Try to fetch an instruction
584 // set up memory request for instruction fetch
586 #define IFETCH_FLAGS(pc) ((pc) & 1) ? PHYSICAL : 0
588 #define IFETCH_FLAGS(pc) 0
592 memReq
->reset(xc
->regs
.pc
& ~3, sizeof(uint32_t),
593 IFETCH_FLAGS(xc
->regs
.pc
));
595 fault
= xc
->translateInstReq(memReq
);
597 if (fault
== No_Fault
)
598 fault
= xc
->mem
->read(memReq
, inst
);
600 if (icacheInterface
&& fault
== No_Fault
) {
601 memReq
->completionEvent
= NULL
;
603 memReq
->time
= curTick
;
604 memReq
->flags
&= ~UNCACHEABLE
;
605 MemAccessResult result
= icacheInterface
->access(memReq
);
607 // Ugly hack to get an event scheduled *only* if the access is
608 // a miss. We really should add first-class support for this
610 if (result
!= MA_HIT
&& icacheInterface
->doEvents
) {
611 memReq
->completionEvent
= &cacheCompletionEvent
;
612 setStatus(IcacheMissStall
);
618 // If we've got a valid instruction (i.e., no fault on instruction
619 // fetch), then execute it.
620 if (fault
== No_Fault
) {
622 // keep an instruction count
625 // check for instruction-count-based events
626 comInsnEventQueue
[0]->serviceEvents(numInst
);
628 // decode the instruction
629 StaticInstPtr
<TheISA
> si(inst
);
631 traceData
= Trace::getInstRecord(curTick
, xc
, this, si
,
635 xc
->regs
.opcode
= (inst
>> 26) & 0x3f;
636 xc
->regs
.ra
= (inst
>> 21) & 0x1f;
637 #endif // FULL_SYSTEM
641 fault
= si
->execute(this, xc
, traceData
);
643 if (si
->isMemRef()) {
649 comLoadEventQueue
[0]->serviceEvents(numLoad
);
653 traceData
->finalize();
655 } // if (fault == No_Fault)
657 if (fault
!= No_Fault
) {
660 #else // !FULL_SYSTEM
661 fatal("fault (%d) detected @ PC 0x%08p", fault
, xc
->regs
.pc
);
662 #endif // FULL_SYSTEM
665 // go to the next instruction
666 xc
->regs
.pc
= xc
->regs
.npc
;
667 xc
->regs
.npc
+= sizeof(MachInst
);
674 system
->pcEventQueue
.service(xc
);
675 } while (oldpc
!= xc
->regs
.pc
);
678 assert(status() == Running
||
680 status() == DcacheMissStall
);
682 if (status() == Running
&& !tickEvent
.scheduled())
683 tickEvent
.schedule(curTick
+ 1);
687 ////////////////////////////////////////////////////////////////////////
689 // SimpleCPU Simulation Object
691 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
693 Param
<Counter
> max_insts_any_thread
;
694 Param
<Counter
> max_insts_all_threads
;
695 Param
<Counter
> max_loads_any_thread
;
696 Param
<Counter
> max_loads_all_threads
;
699 SimObjectParam
<AlphaItb
*> itb
;
700 SimObjectParam
<AlphaDtb
*> dtb
;
701 SimObjectParam
<FunctionalMemory
*> mem
;
702 SimObjectParam
<System
*> system
;
706 SimObjectParam
<Process
*> workload
;
707 #endif // FULL_SYSTEM
709 SimObjectParam
<BaseMem
*> icache
;
710 SimObjectParam
<BaseMem
*> dcache
;
712 END_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
714 BEGIN_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
716 INIT_PARAM_DFLT(max_insts_any_thread
,
717 "terminate when any thread reaches this insn count",
719 INIT_PARAM_DFLT(max_insts_all_threads
,
720 "terminate when all threads have reached this insn count",
722 INIT_PARAM_DFLT(max_loads_any_thread
,
723 "terminate when any thread reaches this load count",
725 INIT_PARAM_DFLT(max_loads_all_threads
,
726 "terminate when all threads have reached this load count",
730 INIT_PARAM(itb
, "Instruction TLB"),
731 INIT_PARAM(dtb
, "Data TLB"),
732 INIT_PARAM(mem
, "memory"),
733 INIT_PARAM(system
, "system object"),
734 INIT_PARAM_DFLT(cpu_id
, "CPU identification number", 0),
735 INIT_PARAM_DFLT(mult
, "system clock multiplier", 1),
737 INIT_PARAM(workload
, "processes to run"),
738 #endif // FULL_SYSTEM
740 INIT_PARAM_DFLT(icache
, "L1 instruction cache object", NULL
),
741 INIT_PARAM_DFLT(dcache
, "L1 data cache object", NULL
)
743 END_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
746 CREATE_SIM_OBJECT(SimpleCPU
)
750 panic("processor clock multiplier must be 1\n");
752 return new SimpleCPU(getInstanceName(), system
,
753 max_insts_any_thread
, max_insts_all_threads
,
754 max_loads_any_thread
, max_loads_all_threads
,
756 (icache
) ? icache
->getInterface() : NULL
,
757 (dcache
) ? dcache
->getInterface() : NULL
,
758 cpu_id
, ticksPerSecond
* mult
);
761 return new SimpleCPU(getInstanceName(), workload
,
762 max_insts_any_thread
, max_insts_all_threads
,
763 max_loads_any_thread
, max_loads_all_threads
,
764 icache
->getInterface(), dcache
->getInterface());
766 #endif // FULL_SYSTEM
769 REGISTER_SIM_OBJECT("SimpleCPU", SimpleCPU
)