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 AlphaItb
*itb
, AlphaDtb
*dtb
,
108 FunctionalMemory
*mem
,
109 MemInterface
*icache_interface
,
110 MemInterface
*dcache_interface
,
111 int cpu_id
, Tick freq
)
112 : BaseCPU(_name
, /* number_of_threads */ 1,
113 max_insts_any_thread
, max_insts_all_threads
,
114 _system
, cpu_id
, freq
),
116 SimpleCPU::SimpleCPU(const string
&_name
, Process
*_process
,
117 Counter max_insts_any_thread
,
118 Counter max_insts_all_threads
,
119 MemInterface
*icache_interface
,
120 MemInterface
*dcache_interface
)
121 : BaseCPU(_name
, /* number_of_threads */ 1,
122 max_insts_any_thread
, max_insts_all_threads
),
124 tickEvent(this), xc(NULL
), cacheCompletionEvent(this)
127 xc
= new ExecContext(this, 0, system
, itb
, dtb
, mem
, cpu_id
);
132 xc
->setStatus(ExecContext::Unallocated
);
134 //Open a GDB debug session on port (7000 + the cpu_id)
135 (new GDBListener(new RemoteGDB(system
, xc
), 7000 + cpu_id
))->listen();
137 AlphaISA::init(system
->physmem
, &xc
->regs
);
141 IntReg
*ipr
= xc
->regs
.ipr
;
142 ipr
[TheISA::IPR_MCSR
] = 0x6;
144 AlphaISA::swap_palshadow(&xc
->regs
, true);
147 ipr
[TheISA::IPR_PAL_BASE
] + AlphaISA::fault_addr
[fault
];
148 xc
->regs
.npc
= xc
->regs
.pc
+ sizeof(MachInst
);
153 system
->initBootContext(xc
);
157 AlphaISA::init(system
->physmem
, &xc
->regs
);
161 IntReg
*ipr
= xc
->regs
.ipr
;
162 ipr
[TheISA::IPR_MCSR
] = 0x6;
164 AlphaISA::swap_palshadow(&xc
->regs
, true);
166 xc
->regs
.pc
= ipr
[TheISA::IPR_PAL_BASE
] + AlphaISA::fault_addr
[fault
];
167 xc
->regs
.npc
= xc
->regs
.pc
+ sizeof(MachInst
);
170 tickEvent
.schedule(0);
174 xc
= new ExecContext(this, /* thread_num */ 0, _process
, /* asid */ 0);
176 if (xc
->status() == ExecContext::Active
) {
178 tickEvent
.schedule(0);
181 #endif // !FULL_SYSTEM
183 icacheInterface
= icache_interface
;
184 dcacheInterface
= dcache_interface
;
186 memReq
= new MemReq();
195 contexts
.push_back(xc
);
198 SimpleCPU::~SimpleCPU()
203 SimpleCPU::regStats()
208 .name(name() + ".num_insts")
209 .desc("Number of instructions executed")
213 .name(name() + ".num_refs")
214 .desc("Number of memory references")
218 .name(name() + ".idle_cycles")
219 .desc("Number of idle cycles")
223 .name(name() + ".idle_fraction")
224 .desc("Percentage of idle cycles")
228 .name(name() + ".icache_stall_cycles")
229 .desc("ICache total stall cycles")
230 .prereq(icacheStallCycles
)
234 .name(name() + ".dcache_stall_cycles")
235 .desc("DCache total stall cycles")
236 .prereq(dcacheStallCycles
)
239 idleFraction
= idleCycles
/ simTicks
;
241 numInsts
= Statistics::scalar(numInst
);
242 simInsts
+= numInsts
;
246 SimpleCPU::serialize()
252 // do we need this anymore?? egh
253 childOut("itb", xc
->itb
);
254 childOut("dtb", xc
->dtb
);
255 childOut("physmem", physmem
);
259 for (int i
= 0; i
< NumIntRegs
; i
++) {
261 ccprintf(buf
, "R%02d", i
);
262 paramOut(buf
.str(), xc
->regs
.intRegFile
[i
]);
264 for (int i
= 0; i
< NumFloatRegs
; i
++) {
266 ccprintf(buf
, "F%02d", i
);
267 paramOut(buf
.str(), xc
->regs
.floatRegFile
.d
[i
]);
269 // CPUTraitsType::serializeSpecialRegs(getProxy(), xc->regs);
273 SimpleCPU::unserialize(IniFile
&db
, const string
&category
, ConfigNode
*node
)
277 for (int i
= 0; i
< NumIntRegs
; i
++) {
279 ccprintf(buf
, "R%02d", i
);
280 db
.findDefault(category
, buf
.str(), data
);
281 to_number(data
,xc
->regs
.intRegFile
[i
]);
283 for (int i
= 0; i
< NumFloatRegs
; i
++) {
285 ccprintf(buf
, "F%02d", i
);
286 db
.findDefault(category
, buf
.str(), data
);
287 xc
->regs
.floatRegFile
.d
[i
] = strtod(data
.c_str(),NULL
);
290 // Read in Special registers
292 // CPUTraitsType::unserializeSpecialRegs(db,category,node,xc->regs);
296 change_thread_state(int thread_number
, int activate
, int priority
)
300 // precise architected memory state accessor macros
303 SimpleCPU::read(Addr addr
, T
& data
, unsigned flags
)
305 memReq
->reset(addr
, sizeof(T
), flags
);
307 // translate to physical address
308 Fault fault
= xc
->translateDataReadReq(memReq
);
310 // do functional access
311 if (fault
== No_Fault
)
312 fault
= xc
->read(memReq
, data
);
315 traceData
->setAddr(addr
);
316 if (fault
== No_Fault
)
317 traceData
->setData(data
);
320 // if we have a cache, do cache access too
321 if (fault
== No_Fault
&& dcacheInterface
) {
323 memReq
->completionEvent
= NULL
;
324 memReq
->time
= curTick
;
325 memReq
->flags
&= ~UNCACHEABLE
;
326 MemAccessResult result
= dcacheInterface
->access(memReq
);
328 // Ugly hack to get an event scheduled *only* if the access is
329 // a miss. We really should add first-class support for this
331 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
332 memReq
->completionEvent
= &cacheCompletionEvent
;
333 setStatus(DcacheMissStall
);
340 #ifndef DOXYGEN_SHOULD_SKIP_THIS
344 SimpleCPU::read(Addr addr
, uint64_t& data
, unsigned flags
);
348 SimpleCPU::read(Addr addr
, uint32_t& data
, unsigned flags
);
352 SimpleCPU::read(Addr addr
, uint16_t& data
, unsigned flags
);
356 SimpleCPU::read(Addr addr
, uint8_t& data
, unsigned flags
);
358 #endif //DOXYGEN_SHOULD_SKIP_THIS
362 SimpleCPU::read(Addr addr
, double& data
, unsigned flags
)
364 return read(addr
, *(uint64_t*)&data
, flags
);
369 SimpleCPU::read(Addr addr
, float& data
, unsigned flags
)
371 return read(addr
, *(uint32_t*)&data
, flags
);
377 SimpleCPU::read(Addr addr
, int32_t& data
, unsigned flags
)
379 return read(addr
, (uint32_t&)data
, flags
);
385 SimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
388 traceData
->setAddr(addr
);
389 traceData
->setData(data
);
392 memReq
->reset(addr
, sizeof(T
), flags
);
394 // translate to physical address
395 Fault fault
= xc
->translateDataWriteReq(memReq
);
397 // do functional access
398 if (fault
== No_Fault
)
399 fault
= xc
->write(memReq
, data
);
401 if (fault
== No_Fault
&& dcacheInterface
) {
403 memReq
->data
= (uint8_t *)&data
;
404 memReq
->completionEvent
= NULL
;
405 memReq
->time
= curTick
;
406 memReq
->flags
&= ~UNCACHEABLE
;
407 MemAccessResult result
= dcacheInterface
->access(memReq
);
409 // Ugly hack to get an event scheduled *only* if the access is
410 // a miss. We really should add first-class support for this
412 if (result
!= MA_HIT
&& dcacheInterface
->doEvents
) {
413 memReq
->completionEvent
= &cacheCompletionEvent
;
414 setStatus(DcacheMissStall
);
418 if (res
&& (fault
== No_Fault
))
419 *res
= memReq
->result
;
425 #ifndef DOXYGEN_SHOULD_SKIP_THIS
428 SimpleCPU::write(uint64_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
432 SimpleCPU::write(uint32_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
436 SimpleCPU::write(uint16_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
440 SimpleCPU::write(uint8_t data
, Addr addr
, unsigned flags
, uint64_t *res
);
442 #endif //DOXYGEN_SHOULD_SKIP_THIS
446 SimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
448 return write(*(uint64_t*)&data
, addr
, flags
, res
);
453 SimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
455 return write(*(uint32_t*)&data
, addr
, flags
, res
);
461 SimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
463 return write((uint32_t)data
, addr
, flags
, res
);
469 SimpleCPU::dbg_vtophys(Addr addr
)
471 return vtophys(xc
, addr
);
473 #endif // FULL_SYSTEM
479 SimpleCPU::processCacheCompletion()
482 case IcacheMissStall
:
483 icacheStallCycles
+= curTick
- lastIcacheStall
;
484 setStatus(IcacheMissComplete
);
486 case DcacheMissStall
:
487 dcacheStallCycles
+= curTick
- lastDcacheStall
;
491 panic("SimpleCPU::processCacheCompletion: bad state");
498 SimpleCPU::post_interrupt(int int_num
, int index
)
500 BaseCPU::post_interrupt(int_num
, index
);
502 if (xc
->status() == ExecContext::Suspended
) {
503 DPRINTF(IPI
,"Suspended Processor awoke\n");
504 xc
->setStatus(ExecContext::Active
);
505 Annotate::Resume(xc
);
508 #endif // FULL_SYSTEM
510 /* start simulation, program loaded, processor precise state initialized */
517 if (fault
== No_Fault
&& AlphaISA::check_interrupts
&&
518 xc
->cpu
->check_interrupts() &&
519 !PC_PAL(xc
->regs
.pc
) &&
520 status() != IcacheMissComplete
) {
523 AlphaISA::check_interrupts
= 0;
524 IntReg
*ipr
= xc
->regs
.ipr
;
526 if (xc
->regs
.ipr
[TheISA::IPR_SIRR
]) {
527 for (int i
= TheISA::INTLEVEL_SOFTWARE_MIN
;
528 i
< TheISA::INTLEVEL_SOFTWARE_MAX
; i
++) {
529 if (ipr
[TheISA::IPR_SIRR
] & (ULL(1) << i
)) {
530 // See table 4-19 of 21164 hardware reference
531 ipl
= (i
- TheISA::INTLEVEL_SOFTWARE_MIN
) + 1;
532 summary
|= (ULL(1) << i
);
537 uint64_t interrupts
= xc
->cpu
->intr_status();
538 for(int i
= TheISA::INTLEVEL_EXTERNAL_MIN
;
539 i
< TheISA::INTLEVEL_EXTERNAL_MAX
; i
++) {
540 if (interrupts
& (ULL(1) << i
)) {
541 // See table 4-19 of 21164 hardware reference
543 summary
|= (ULL(1) << i
);
547 if (ipr
[TheISA::IPR_ASTRR
])
548 panic("asynchronous traps not implemented\n");
550 if (ipl
&& ipl
> xc
->regs
.ipr
[TheISA::IPR_IPLR
]) {
551 ipr
[TheISA::IPR_ISR
] = summary
;
552 ipr
[TheISA::IPR_INTID
] = ipl
;
553 xc
->ev5_trap(Interrupt_Fault
);
555 DPRINTF(Flow
, "Interrupt! IPLR=%d ipl=%d summary=%x\n",
556 ipr
[TheISA::IPR_IPLR
], ipl
, summary
);
561 // maintain $r0 semantics
562 xc
->regs
.intRegFile
[ZeroReg
] = 0;
564 xc
->regs
.floatRegFile
.d
[ZeroReg
] = 0.0;
565 #endif // TARGET_ALPHA
567 if (status() == IcacheMissComplete
) {
568 // We've already fetched an instruction and were stalled on an
569 // I-cache miss. No need to fetch it again.
574 // Try to fetch an instruction
576 // set up memory request for instruction fetch
578 #define IFETCH_FLAGS(pc) ((pc) & 1) ? PHYSICAL : 0
580 #define IFETCH_FLAGS(pc) 0
584 memReq
->reset(xc
->regs
.pc
& ~3, sizeof(uint32_t),
585 IFETCH_FLAGS(xc
->regs
.pc
));
587 fault
= xc
->translateInstReq(memReq
);
589 if (fault
== No_Fault
)
590 fault
= xc
->mem
->read(memReq
, inst
);
592 if (icacheInterface
&& fault
== No_Fault
) {
593 memReq
->completionEvent
= NULL
;
595 memReq
->time
= curTick
;
596 memReq
->flags
&= ~UNCACHEABLE
;
597 MemAccessResult result
= icacheInterface
->access(memReq
);
599 // Ugly hack to get an event scheduled *only* if the access is
600 // a miss. We really should add first-class support for this
602 if (result
!= MA_HIT
&& icacheInterface
->doEvents
) {
603 memReq
->completionEvent
= &cacheCompletionEvent
;
604 setStatus(IcacheMissStall
);
610 // If we've got a valid instruction (i.e., no fault on instruction
611 // fetch), then execute it.
612 if (fault
== No_Fault
) {
614 // keep an instruction count
617 // check for instruction-count-based events
618 comInsnEventQueue
[0]->serviceEvents(numInst
);
620 // decode the instruction
621 StaticInstPtr
<TheISA
> si(inst
);
623 traceData
= Trace::getInstRecord(curTick
, xc
, this, si
,
627 xc
->regs
.opcode
= (inst
>> 26) & 0x3f;
628 xc
->regs
.ra
= (inst
>> 21) & 0x1f;
629 #endif // FULL_SYSTEM
633 fault
= si
->execute(this, xc
, traceData
);
635 if (si
->isMemRef()) {
640 traceData
->finalize();
642 } // if (fault == No_Fault)
644 if (fault
!= No_Fault
) {
647 #else // !FULL_SYSTEM
648 fatal("fault (%d) detected @ PC 0x%08p", fault
, xc
->regs
.pc
);
649 #endif // FULL_SYSTEM
652 // go to the next instruction
653 xc
->regs
.pc
= xc
->regs
.npc
;
654 xc
->regs
.npc
+= sizeof(MachInst
);
661 system
->pcEventQueue
.service(xc
);
662 } while (oldpc
!= xc
->regs
.pc
);
665 assert(status() == Running
||
667 status() == DcacheMissStall
);
669 if (status() == Running
&& !tickEvent
.scheduled())
670 tickEvent
.schedule(curTick
+ 1);
674 ////////////////////////////////////////////////////////////////////////
676 // SimpleCPU Simulation Object
678 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
680 Param
<Counter
> max_insts_any_thread
;
681 Param
<Counter
> max_insts_all_threads
;
684 SimObjectParam
<AlphaItb
*> itb
;
685 SimObjectParam
<AlphaDtb
*> dtb
;
686 SimObjectParam
<FunctionalMemory
*> mem
;
687 SimObjectParam
<System
*> system
;
691 SimObjectParam
<Process
*> workload
;
692 #endif // FULL_SYSTEM
694 SimObjectParam
<BaseMem
*> icache
;
695 SimObjectParam
<BaseMem
*> dcache
;
697 END_DECLARE_SIM_OBJECT_PARAMS(SimpleCPU
)
699 BEGIN_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
701 INIT_PARAM_DFLT(max_insts_any_thread
,
702 "terminate when any thread reaches this insn count",
704 INIT_PARAM_DFLT(max_insts_all_threads
,
705 "terminate when all threads have reached this insn count",
709 INIT_PARAM(itb
, "Instruction TLB"),
710 INIT_PARAM(dtb
, "Data TLB"),
711 INIT_PARAM(mem
, "memory"),
712 INIT_PARAM(system
, "system object"),
713 INIT_PARAM_DFLT(cpu_id
, "CPU identification number", 0),
714 INIT_PARAM_DFLT(mult
, "system clock multiplier", 1),
716 INIT_PARAM(workload
, "processes to run"),
717 #endif // FULL_SYSTEM
719 INIT_PARAM_DFLT(icache
, "L1 instruction cache object", NULL
),
720 INIT_PARAM_DFLT(dcache
, "L1 data cache object", NULL
)
722 END_INIT_SIM_OBJECT_PARAMS(SimpleCPU
)
725 CREATE_SIM_OBJECT(SimpleCPU
)
729 panic("processor clock multiplier must be 1\n");
731 return new SimpleCPU(getInstanceName(), system
,
732 max_insts_any_thread
, max_insts_all_threads
,
734 (icache
) ? icache
->getInterface() : NULL
,
735 (dcache
) ? dcache
->getInterface() : NULL
,
736 cpu_id
, ticksPerSecond
* mult
);
739 return new SimpleCPU(getInstanceName(), workload
,
740 max_insts_any_thread
, max_insts_all_threads
,
741 icache
->getInterface(), dcache
->getInterface());
743 #endif // FULL_SYSTEM
746 REGISTER_SIM_OBJECT("SimpleCPU", SimpleCPU
)