2 * Copyright (c) 2010-2011 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * Authors: Steve Reinhardt
43 #include "arch/faults.hh"
44 #include "arch/utility.hh"
45 #include "base/loader/symtab.hh"
46 #include "base/cp_annotate.hh"
47 #include "base/cprintf.hh"
48 #include "base/inifile.hh"
49 #include "base/misc.hh"
50 #include "base/pollevent.hh"
51 #include "base/range.hh"
52 #include "base/trace.hh"
53 #include "base/types.hh"
54 #include "config/the_isa.hh"
55 #include "config/use_checker.hh"
56 #include "cpu/simple/base.hh"
57 #include "cpu/base.hh"
58 #include "cpu/exetrace.hh"
59 #include "cpu/profile.hh"
60 #include "cpu/simple_thread.hh"
62 #include "cpu/static_inst.hh"
63 #include "cpu/thread_context.hh"
64 #include "debug/Decode.hh"
65 #include "debug/Fetch.hh"
66 #include "debug/Quiesce.hh"
67 #include "mem/packet.hh"
68 #include "mem/request.hh"
69 #include "params/BaseSimpleCPU.hh"
70 #include "sim/byteswap.hh"
71 #include "sim/debug.hh"
72 #include "sim/sim_events.hh"
73 #include "sim/sim_object.hh"
74 #include "sim/stats.hh"
75 #include "sim/system.hh"
78 #include "arch/kernel_stats.hh"
79 #include "arch/stacktrace.hh"
80 #include "arch/tlb.hh"
81 #include "arch/vtophys.hh"
83 #include "mem/mem_object.hh"
87 #include "cpu/checker/cpu.hh"
88 #include "cpu/checker/thread_context.hh"
92 using namespace TheISA
;
94 BaseSimpleCPU::BaseSimpleCPU(BaseSimpleCPUParams
*p
)
95 : BaseCPU(p
), traceData(NULL
), thread(NULL
), predecoder(NULL
)
98 thread
= new SimpleThread(this, 0, p
->system
, p
->itb
, p
->dtb
);
100 thread
= new SimpleThread(this, /* thread_num */ 0, p
->workload
[0],
102 #endif // !FULL_SYSTEM
104 thread
->setStatus(ThreadContext::Halted
);
106 tc
= thread
->getTC();
110 BaseCPU
*temp_checker
= p
->checker
;
111 checker
= dynamic_cast<CheckerCPU
*>(temp_checker
);
113 checker
->setSystem(p
->system
);
115 // Manipulate thread context
116 ThreadContext
*cpu_tc
= tc
;
117 tc
= new CheckerThreadContext
<ThreadContext
>(cpu_tc
, this->checker
);
130 threadContexts
.push_back(tc
);
137 BaseSimpleCPU::~BaseSimpleCPU()
142 BaseSimpleCPU::deallocateContext(int thread_num
)
144 // for now, these are equivalent
145 suspendContext(thread_num
);
150 BaseSimpleCPU::haltContext(int thread_num
)
152 // for now, these are equivalent
153 suspendContext(thread_num
);
158 BaseSimpleCPU::regStats()
160 using namespace Stats
;
165 .name(name() + ".num_insts")
166 .desc("Number of instructions executed")
170 .name(name() + ".num_int_alu_accesses")
171 .desc("Number of integer alu accesses")
175 .name(name() + ".num_fp_alu_accesses")
176 .desc("Number of float alu accesses")
180 .name(name() + ".num_func_calls")
181 .desc("number of times a function call or return occured")
185 .name(name() + ".num_conditional_control_insts")
186 .desc("number of instructions that are conditional controls")
190 .name(name() + ".num_int_insts")
191 .desc("number of integer instructions")
195 .name(name() + ".num_fp_insts")
196 .desc("number of float instructions")
200 .name(name() + ".num_int_register_reads")
201 .desc("number of times the integer registers were read")
205 .name(name() + ".num_int_register_writes")
206 .desc("number of times the integer registers were written")
210 .name(name() + ".num_fp_register_reads")
211 .desc("number of times the floating registers were read")
215 .name(name() + ".num_fp_register_writes")
216 .desc("number of times the floating registers were written")
220 .name(name()+".num_mem_refs")
221 .desc("number of memory refs")
225 .name(name() + ".num_store_insts")
226 .desc("Number of store instructions")
230 .name(name() + ".num_load_insts")
231 .desc("Number of load instructions")
235 .name(name() + ".not_idle_fraction")
236 .desc("Percentage of non-idle cycles")
240 .name(name() + ".idle_fraction")
241 .desc("Percentage of idle cycles")
245 .name(name() + ".num_busy_cycles")
246 .desc("Number of busy cycles")
250 .name(name()+".num_idle_cycles")
251 .desc("Number of idle cycles")
255 .name(name() + ".icache_stall_cycles")
256 .desc("ICache total stall cycles")
257 .prereq(icacheStallCycles
)
261 .name(name() + ".dcache_stall_cycles")
262 .desc("DCache total stall cycles")
263 .prereq(dcacheStallCycles
)
267 .name(name() + ".icache_retry_cycles")
268 .desc("ICache total retry cycles")
269 .prereq(icacheRetryCycles
)
273 .name(name() + ".dcache_retry_cycles")
274 .desc("DCache total retry cycles")
275 .prereq(dcacheRetryCycles
)
278 idleFraction
= constant(1.0) - notIdleFraction
;
279 numIdleCycles
= idleFraction
* numCycles
;
280 numBusyCycles
= (notIdleFraction
)*numCycles
;
284 BaseSimpleCPU::resetStats()
286 // startNumInst = numInst;
287 notIdleFraction
= (_status
!= Idle
);
291 BaseSimpleCPU::serialize(ostream
&os
)
293 SERIALIZE_ENUM(_status
);
294 BaseCPU::serialize(os
);
295 // SERIALIZE_SCALAR(inst);
296 nameOut(os
, csprintf("%s.xc.0", name()));
297 thread
->serialize(os
);
301 BaseSimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
303 UNSERIALIZE_ENUM(_status
);
304 BaseCPU::unserialize(cp
, section
);
305 // UNSERIALIZE_SCALAR(inst);
306 thread
->unserialize(cp
, csprintf("%s.xc.0", section
));
310 change_thread_state(ThreadID tid
, int activate
, int priority
)
316 BaseSimpleCPU::dbg_vtophys(Addr addr
)
318 return vtophys(tc
, addr
);
320 #endif // FULL_SYSTEM
324 BaseSimpleCPU::wakeup()
326 if (thread
->status() != ThreadContext::Suspended
)
329 DPRINTF(Quiesce
,"Suspended Processor awoke\n");
332 #endif // FULL_SYSTEM
335 BaseSimpleCPU::checkForInterrupts()
338 if (checkInterrupts(tc
)) {
339 Fault interrupt
= interrupts
->getInterrupt(tc
);
341 if (interrupt
!= NoFault
) {
343 interrupts
->updateIntrInfo(tc
);
344 interrupt
->invoke(tc
);
353 BaseSimpleCPU::setupFetchRequest(Request
*req
)
355 Addr instAddr
= thread
->instAddr();
357 // set up memory request for instruction fetch
358 DPRINTF(Fetch
, "Fetch: PC:%08p\n", instAddr
);
360 Addr fetchPC
= (instAddr
& PCMask
) + fetchOffset
;
361 req
->setVirt(0, fetchPC
, sizeof(MachInst
), Request::INST_FETCH
, instAddr
);
366 BaseSimpleCPU::preExecute()
368 // maintain $r0 semantics
369 thread
->setIntReg(ZeroReg
, 0);
370 #if THE_ISA == ALPHA_ISA
371 thread
->setFloatReg(ZeroReg
, 0.0);
374 // check for instruction-count-based events
375 comInstEventQueue
[0]->serviceEvents(numInst
);
376 system
->instEventQueue
.serviceEvents(system
->totalNumInsts
);
378 // decode the instruction
381 TheISA::PCState pcState
= thread
->pcState();
383 if (isRomMicroPC(pcState
.microPC())) {
385 curStaticInst
= microcodeRom
.fetchMicroop(pcState
.microPC(),
387 } else if (!curMacroStaticInst
) {
388 //We're not in the middle of a macro instruction
389 StaticInstPtr instPtr
= NULL
;
391 //Predecode, ie bundle up an ExtMachInst
392 //This should go away once the constructor can be set up properly
393 predecoder
.setTC(thread
->getTC());
394 //If more fetch data is needed, pass it in.
395 Addr fetchPC
= (pcState
.instAddr() & PCMask
) + fetchOffset
;
396 //if(predecoder.needMoreBytes())
397 predecoder
.moreBytes(pcState
, fetchPC
, inst
);
399 // predecoder.process();
401 //If an instruction is ready, decode it. Otherwise, we'll have to
402 //fetch beyond the MachInst at the current pc.
403 if (predecoder
.extMachInstReady()) {
405 ExtMachInst machInst
= predecoder
.getExtMachInst(pcState
);
406 thread
->pcState(pcState
);
407 instPtr
= thread
->decoder
.decode(machInst
, pcState
.instAddr());
410 fetchOffset
+= sizeof(MachInst
);
413 //If we decoded an instruction and it's microcoded, start pulling
415 if (instPtr
&& instPtr
->isMacroop()) {
416 curMacroStaticInst
= instPtr
;
417 curStaticInst
= curMacroStaticInst
->fetchMicroop(pcState
.microPC());
419 curStaticInst
= instPtr
;
422 //Read the next micro op from the macro op
423 curStaticInst
= curMacroStaticInst
->fetchMicroop(pcState
.microPC());
426 //If we decoded an instruction this "tick", record information about it.
430 traceData
= tracer
->getInstRecord(curTick(), tc
,
431 curStaticInst
, thread
->pcState(), curMacroStaticInst
);
433 DPRINTF(Decode
,"Decode: Decoded %s instruction: 0x%x\n",
434 curStaticInst
->getName(), curStaticInst
->machInst
);
440 BaseSimpleCPU::postExecute()
442 assert(curStaticInst
);
444 TheISA::PCState pc
= tc
->pcState();
445 Addr instAddr
= pc
.instAddr();
447 if (thread
->profile
) {
448 bool usermode
= TheISA::inUserMode(tc
);
449 thread
->profilePC
= usermode
? 1 : instAddr
;
450 ProfileNode
*node
= thread
->profile
->consume(tc
, curStaticInst
);
452 thread
->profileNode
= node
;
456 if (curStaticInst
->isMemRef()) {
460 if (curStaticInst
->isLoad()) {
462 comLoadEventQueue
[0]->serviceEvents(numLoad
);
465 if (CPA::available()) {
466 CPA::cpa()->swAutoBegin(tc
, pc
.nextInstAddr());
469 /* Power model statistics */
470 //integer alu accesses
471 if (curStaticInst
->isInteger()){
477 if (curStaticInst
->isFloating()){
482 //number of function calls/returns to get window accesses
483 if (curStaticInst
->isCall() || curStaticInst
->isReturn()){
487 //the number of branch predictions that will be made
488 if (curStaticInst
->isCondCtrl()){
493 if (curStaticInst
->isLoad()){
497 if (curStaticInst
->isStore()){
500 /* End power model statistics */
502 traceFunctions(instAddr
);
513 BaseSimpleCPU::advancePC(Fault fault
)
515 //Since we're moving to a new pc, zero out the offset
517 if (fault
!= NoFault
) {
518 curMacroStaticInst
= StaticInst::nullStaticInstPtr
;
519 fault
->invoke(tc
, curStaticInst
);
523 if (curStaticInst
->isLastMicroop())
524 curMacroStaticInst
= StaticInst::nullStaticInstPtr
;
525 TheISA::PCState pcState
= thread
->pcState();
526 TheISA::advancePC(pcState
, curStaticInst
);
527 thread
->pcState(pcState
);
533 BaseSimpleCPU::CacheOp(uint8_t Op, Addr EffAddr)
535 // translate to physical address
536 Fault fault = NoFault;
537 int CacheID = Op & 0x3; // Lower 3 bits identify Cache
538 int CacheOP = Op >> 2; // Upper 3 bits identify Cache Operation
541 warn("CacheOps not implemented for secondary/tertiary caches\n");
546 { // Fill Packet Type
547 case 0: warn("Invalidate Cache Op\n");
549 case 1: warn("Index Load Tag Cache Op\n");
551 case 2: warn("Index Store Tag Cache Op\n");
553 case 4: warn("Hit Invalidate Cache Op\n");
555 case 5: warn("Fill/Hit Writeback Invalidate Cache Op\n");
557 case 6: warn("Hit Writeback\n");
559 case 7: warn("Fetch & Lock Cache Op\n");
561 default: warn("Unimplemented Cache Op\n");