2 * Copyright (c) 2010 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/cp_annotate.hh"
46 #include "base/cprintf.hh"
47 #include "base/inifile.hh"
48 #include "base/loader/symtab.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 "cpu/base.hh"
56 #include "cpu/exetrace.hh"
57 #include "cpu/profile.hh"
58 #include "cpu/simple/base.hh"
59 #include "cpu/simple_thread.hh"
61 #include "cpu/static_inst.hh"
62 #include "cpu/thread_context.hh"
63 #include "mem/packet.hh"
64 #include "mem/request.hh"
65 #include "params/BaseSimpleCPU.hh"
66 #include "sim/byteswap.hh"
67 #include "sim/debug.hh"
68 #include "sim/sim_events.hh"
69 #include "sim/sim_object.hh"
70 #include "sim/stats.hh"
71 #include "sim/system.hh"
74 #include "arch/kernel_stats.hh"
75 #include "arch/stacktrace.hh"
76 #include "arch/tlb.hh"
77 #include "arch/vtophys.hh"
79 #include "mem/mem_object.hh"
83 using namespace TheISA
;
85 BaseSimpleCPU::BaseSimpleCPU(BaseSimpleCPUParams
*p
)
86 : BaseCPU(p
), traceData(NULL
), thread(NULL
), predecoder(NULL
)
89 thread
= new SimpleThread(this, 0, p
->system
, p
->itb
, p
->dtb
);
91 thread
= new SimpleThread(this, /* thread_num */ 0, p
->workload
[0],
93 #endif // !FULL_SYSTEM
95 thread
->setStatus(ThreadContext::Halted
);
106 threadContexts
.push_back(tc
);
113 BaseSimpleCPU::~BaseSimpleCPU()
118 BaseSimpleCPU::deallocateContext(int thread_num
)
120 // for now, these are equivalent
121 suspendContext(thread_num
);
126 BaseSimpleCPU::haltContext(int thread_num
)
128 // for now, these are equivalent
129 suspendContext(thread_num
);
134 BaseSimpleCPU::regStats()
136 using namespace Stats
;
141 .name(name() + ".num_insts")
142 .desc("Number of instructions executed")
146 .name(name() + ".num_refs")
147 .desc("Number of memory references")
151 .name(name() + ".not_idle_fraction")
152 .desc("Percentage of non-idle cycles")
156 .name(name() + ".idle_fraction")
157 .desc("Percentage of idle cycles")
161 .name(name() + ".icache_stall_cycles")
162 .desc("ICache total stall cycles")
163 .prereq(icacheStallCycles
)
167 .name(name() + ".dcache_stall_cycles")
168 .desc("DCache total stall cycles")
169 .prereq(dcacheStallCycles
)
173 .name(name() + ".icache_retry_cycles")
174 .desc("ICache total retry cycles")
175 .prereq(icacheRetryCycles
)
179 .name(name() + ".dcache_retry_cycles")
180 .desc("DCache total retry cycles")
181 .prereq(dcacheRetryCycles
)
184 idleFraction
= constant(1.0) - notIdleFraction
;
188 BaseSimpleCPU::resetStats()
190 // startNumInst = numInst;
191 notIdleFraction
= (_status
!= Idle
);
195 BaseSimpleCPU::serialize(ostream
&os
)
197 SERIALIZE_ENUM(_status
);
198 BaseCPU::serialize(os
);
199 // SERIALIZE_SCALAR(inst);
200 nameOut(os
, csprintf("%s.xc.0", name()));
201 thread
->serialize(os
);
205 BaseSimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
207 UNSERIALIZE_ENUM(_status
);
208 BaseCPU::unserialize(cp
, section
);
209 // UNSERIALIZE_SCALAR(inst);
210 thread
->unserialize(cp
, csprintf("%s.xc.0", section
));
214 change_thread_state(ThreadID tid
, int activate
, int priority
)
219 BaseSimpleCPU::prefetch(Addr addr
, unsigned flags
)
222 traceData
->setAddr(addr
);
225 // need to do this...
229 BaseSimpleCPU::writeHint(Addr addr
, int size
, unsigned flags
)
232 traceData
->setAddr(addr
);
235 // need to do this...
240 BaseSimpleCPU::copySrcTranslate(Addr src
)
243 static bool no_warn
= true;
245 (dcacheInterface
) ? dcacheInterface
->getBlockSize() : 64;
246 // Only support block sizes of 64 atm.
247 assert(blk_size
== 64);
248 int offset
= src
& (blk_size
- 1);
250 // Make sure block doesn't span page
252 (src
& PageMask
) != ((src
+ blk_size
) & PageMask
) &&
253 (src
>> 40) != 0xfffffc) {
254 warn("Copied block source spans pages %x.", src
);
258 memReq
->reset(src
& ~(blk_size
- 1), blk_size
);
260 // translate to physical address
261 Fault fault
= thread
->translateDataReadReq(req
);
263 if (fault
== NoFault
) {
264 thread
->copySrcAddr
= src
;
265 thread
->copySrcPhysAddr
= memReq
->paddr
+ offset
;
267 assert(!fault
->isAlignmentFault());
269 thread
->copySrcAddr
= 0;
270 thread
->copySrcPhysAddr
= 0;
279 BaseSimpleCPU::copy(Addr dest
)
282 static bool no_warn
= true;
284 (dcacheInterface
) ? dcacheInterface
->getBlockSize() : 64;
285 // Only support block sizes of 64 atm.
286 assert(blk_size
== 64);
287 uint8_t data
[blk_size
];
288 //assert(thread->copySrcAddr);
289 int offset
= dest
& (blk_size
- 1);
291 // Make sure block doesn't span page
293 (dest
& PageMask
) != ((dest
+ blk_size
) & PageMask
) &&
294 (dest
>> 40) != 0xfffffc) {
296 warn("Copied block destination spans pages %x. ", dest
);
299 memReq
->reset(dest
& ~(blk_size
-1), blk_size
);
300 // translate to physical address
301 Fault fault
= thread
->translateDataWriteReq(req
);
303 if (fault
== NoFault
) {
304 Addr dest_addr
= memReq
->paddr
+ offset
;
305 // Need to read straight from memory since we have more than 8 bytes.
306 memReq
->paddr
= thread
->copySrcPhysAddr
;
307 thread
->mem
->read(memReq
, data
);
308 memReq
->paddr
= dest_addr
;
309 thread
->mem
->write(memReq
, data
);
310 if (dcacheInterface
) {
312 memReq
->completionEvent
= NULL
;
313 memReq
->paddr
= thread
->copySrcPhysAddr
;
314 memReq
->dest
= dest_addr
;
316 memReq
->time
= curTick
;
317 dcacheInterface
->access(memReq
);
321 assert(!fault
->isAlignmentFault());
325 panic("copy not implemented");
332 BaseSimpleCPU::dbg_vtophys(Addr addr
)
334 return vtophys(tc
, addr
);
336 #endif // FULL_SYSTEM
340 BaseSimpleCPU::wakeup()
342 if (thread
->status() != ThreadContext::Suspended
)
345 DPRINTF(Quiesce
,"Suspended Processor awoke\n");
348 #endif // FULL_SYSTEM
351 BaseSimpleCPU::checkForInterrupts()
354 if (checkInterrupts(tc
)) {
355 Fault interrupt
= interrupts
->getInterrupt(tc
);
357 if (interrupt
!= NoFault
) {
359 interrupts
->updateIntrInfo(tc
);
360 interrupt
->invoke(tc
);
369 BaseSimpleCPU::setupFetchRequest(Request
*req
)
371 Addr threadPC
= thread
->readPC();
373 // set up memory request for instruction fetch
374 #if ISA_HAS_DELAY_SLOT
375 DPRINTF(Fetch
,"Fetch: PC:%08p NPC:%08p NNPC:%08p\n",threadPC
,
376 thread
->readNextPC(),thread
->readNextNPC());
378 DPRINTF(Fetch
,"Fetch: PC:%08p NPC:%08p\n",threadPC
,
379 thread
->readNextPC());
382 Addr fetchPC
= (threadPC
& PCMask
) + fetchOffset
;
383 req
->setVirt(0, fetchPC
, sizeof(MachInst
), Request::INST_FETCH
, threadPC
);
388 BaseSimpleCPU::preExecute()
390 // maintain $r0 semantics
391 thread
->setIntReg(ZeroReg
, 0);
392 #if THE_ISA == ALPHA_ISA
393 thread
->setFloatReg(ZeroReg
, 0.0);
396 // check for instruction-count-based events
397 comInstEventQueue
[0]->serviceEvents(numInst
);
399 // decode the instruction
402 MicroPC upc
= thread
->readMicroPC();
404 if (isRomMicroPC(upc
)) {
406 curStaticInst
= microcodeRom
.fetchMicroop(upc
, curMacroStaticInst
);
407 } else if (!curMacroStaticInst
) {
408 //We're not in the middle of a macro instruction
409 StaticInstPtr instPtr
= NULL
;
411 //Predecode, ie bundle up an ExtMachInst
412 //This should go away once the constructor can be set up properly
413 predecoder
.setTC(thread
->getTC());
414 //If more fetch data is needed, pass it in.
415 Addr fetchPC
= (thread
->readPC() & PCMask
) + fetchOffset
;
416 //if(predecoder.needMoreBytes())
417 predecoder
.moreBytes(thread
->readPC(), fetchPC
, inst
);
419 // predecoder.process();
421 //If an instruction is ready, decode it. Otherwise, we'll have to
422 //fetch beyond the MachInst at the current pc.
423 if (predecoder
.extMachInstReady()) {
424 #if THE_ISA == X86_ISA || THE_ISA == ARM_ISA
425 thread
->setNextPC(thread
->readPC() + predecoder
.getInstSize());
428 instPtr
= StaticInst::decode(predecoder
.getExtMachInst(),
432 fetchOffset
+= sizeof(MachInst
);
435 //If we decoded an instruction and it's microcoded, start pulling
437 if (instPtr
&& instPtr
->isMacroop()) {
438 curMacroStaticInst
= instPtr
;
439 curStaticInst
= curMacroStaticInst
->fetchMicroop(upc
);
441 curStaticInst
= instPtr
;
444 //Read the next micro op from the macro op
445 curStaticInst
= curMacroStaticInst
->fetchMicroop(upc
);
448 //If we decoded an instruction this "tick", record information about it.
452 traceData
= tracer
->getInstRecord(curTick
, tc
,
453 curStaticInst
, thread
->readPC(),
454 curMacroStaticInst
, thread
->readMicroPC());
456 DPRINTF(Decode
,"Decode: Decoded %s instruction: 0x%x\n",
457 curStaticInst
->getName(), curStaticInst
->machInst
);
461 thread
->setInst(inst
);
462 #endif // FULL_SYSTEM
467 BaseSimpleCPU::postExecute()
470 if (thread
->profile
&& curStaticInst
) {
471 bool usermode
= TheISA::inUserMode(tc
);
472 thread
->profilePC
= usermode
? 1 : thread
->readPC();
473 ProfileNode
*node
= thread
->profile
->consume(tc
, curStaticInst
);
475 thread
->profileNode
= node
;
479 if (curStaticInst
->isMemRef()) {
483 if (curStaticInst
->isLoad()) {
485 comLoadEventQueue
[0]->serviceEvents(numLoad
);
488 if (CPA::available()) {
489 CPA::cpa()->swAutoBegin(tc
, thread
->readNextPC());
492 traceFunctions(thread
->readPC());
503 BaseSimpleCPU::advancePC(Fault fault
)
505 //Since we're moving to a new pc, zero out the offset
507 if (fault
!= NoFault
) {
508 curMacroStaticInst
= StaticInst::nullStaticInstPtr
;
512 //If we're at the last micro op for this instruction
513 if (curStaticInst
&& curStaticInst
->isLastMicroop()) {
514 //We should be working with a macro op or be in the ROM
515 assert(curMacroStaticInst
||
516 isRomMicroPC(thread
->readMicroPC()));
517 //Close out this macro op, and clean up the
519 curMacroStaticInst
= StaticInst::nullStaticInstPtr
;
520 thread
->setMicroPC(normalMicroPC(0));
521 thread
->setNextMicroPC(normalMicroPC(1));
523 //If we're still in a macro op
524 if (curMacroStaticInst
|| isRomMicroPC(thread
->readMicroPC())) {
525 //Advance the micro pc
526 thread
->setMicroPC(thread
->readNextMicroPC());
527 //Advance the "next" micro pc. Note that there are no delay
528 //slots, and micro ops are "word" addressed.
529 thread
->setNextMicroPC(thread
->readNextMicroPC() + 1);
531 // go to the next instruction
532 thread
->setPC(thread
->readNextPC());
533 thread
->setNextPC(thread
->readNextNPC());
534 thread
->setNextNPC(thread
->readNextNPC() + sizeof(MachInst
));
535 assert(thread
->readNextPC() != thread
->readNextNPC());
541 BaseSimpleCPU::CacheOp(uint8_t Op, Addr EffAddr)
543 // translate to physical address
544 Fault fault = NoFault;
545 int CacheID = Op & 0x3; // Lower 3 bits identify Cache
546 int CacheOP = Op >> 2; // Upper 3 bits identify Cache Operation
549 warn("CacheOps not implemented for secondary/tertiary caches\n");
554 { // Fill Packet Type
555 case 0: warn("Invalidate Cache Op\n");
557 case 1: warn("Index Load Tag Cache Op\n");
559 case 2: warn("Index Store Tag Cache Op\n");
561 case 4: warn("Hit Invalidate Cache Op\n");
563 case 5: warn("Fill/Hit Writeback Invalidate Cache Op\n");
565 case 6: warn("Hit Writeback\n");
567 case 7: warn("Fetch & Lock Cache Op\n");
569 default: warn("Unimplemented Cache Op\n");