232554db2ee1a54bab372af0cb8c912c2d7feb9d
[gem5.git] / src / cpu / inorder / cpu.cc
1 /*
2 * Copyright (c) 2007 MIPS Technologies, Inc.
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Korey Sewell
29 *
30 */
31
32 #include <algorithm>
33
34 #include "arch/utility.hh"
35 #include "base/bigint.hh"
36 #include "config/full_system.hh"
37 #include "config/the_isa.hh"
38 #include "cpu/inorder/resources/resource_list.hh"
39 #include "cpu/inorder/cpu.hh"
40 #include "cpu/inorder/first_stage.hh"
41 #include "cpu/inorder/inorder_dyn_inst.hh"
42 #include "cpu/inorder/pipeline_traits.hh"
43 #include "cpu/inorder/resource_pool.hh"
44 #include "cpu/inorder/thread_context.hh"
45 #include "cpu/inorder/thread_state.hh"
46 #include "cpu/activity.hh"
47 #include "cpu/base.hh"
48 #include "cpu/exetrace.hh"
49 #include "cpu/simple_thread.hh"
50 #include "cpu/thread_context.hh"
51 #include "debug/Activity.hh"
52 #include "debug/InOrderCPU.hh"
53 #include "debug/RefCount.hh"
54 #include "debug/SkedCache.hh"
55 #include "debug/Quiesce.hh"
56 #include "mem/translating_port.hh"
57 #include "params/InOrderCPU.hh"
58 #include "sim/process.hh"
59 #include "sim/stat_control.hh"
60
61 #if FULL_SYSTEM
62 #include "cpu/quiesce_event.hh"
63 #include "sim/system.hh"
64 #endif
65
66 #if THE_ISA == ALPHA_ISA
67 #include "arch/alpha/osfpal.hh"
68 #endif
69
70 using namespace std;
71 using namespace TheISA;
72 using namespace ThePipeline;
73
74 InOrderCPU::TickEvent::TickEvent(InOrderCPU *c)
75 : Event(CPU_Tick_Pri), cpu(c)
76 { }
77
78
79 void
80 InOrderCPU::TickEvent::process()
81 {
82 cpu->tick();
83 }
84
85
86 const char *
87 InOrderCPU::TickEvent::description()
88 {
89 return "InOrderCPU tick event";
90 }
91
92 InOrderCPU::CPUEvent::CPUEvent(InOrderCPU *_cpu, CPUEventType e_type,
93 Fault fault, ThreadID _tid, DynInstPtr inst,
94 CPUEventPri event_pri)
95 : Event(event_pri), cpu(_cpu)
96 {
97 setEvent(e_type, fault, _tid, inst);
98 }
99
100
101 std::string InOrderCPU::eventNames[NumCPUEvents] =
102 {
103 "ActivateThread",
104 "ActivateNextReadyThread",
105 "DeactivateThread",
106 "HaltThread",
107 "SuspendThread",
108 "Trap",
109 "Syscall",
110 "SquashFromMemStall",
111 "UpdatePCs"
112 };
113
114 void
115 InOrderCPU::CPUEvent::process()
116 {
117 switch (cpuEventType)
118 {
119 case ActivateThread:
120 cpu->activateThread(tid);
121 cpu->resPool->activateThread(tid);
122 break;
123
124 case ActivateNextReadyThread:
125 cpu->activateNextReadyThread();
126 break;
127
128 case DeactivateThread:
129 cpu->deactivateThread(tid);
130 cpu->resPool->deactivateThread(tid);
131 break;
132
133 case HaltThread:
134 cpu->haltThread(tid);
135 cpu->resPool->deactivateThread(tid);
136 break;
137
138 case SuspendThread:
139 cpu->suspendThread(tid);
140 cpu->resPool->suspendThread(tid);
141 break;
142
143 case SquashFromMemStall:
144 cpu->squashDueToMemStall(inst->squashingStage, inst->seqNum, tid);
145 cpu->resPool->squashDueToMemStall(inst, inst->squashingStage,
146 inst->seqNum, tid);
147 break;
148
149 case Trap:
150 DPRINTF(InOrderCPU, "Trapping CPU\n");
151 cpu->trap(fault, tid, inst);
152 cpu->resPool->trap(fault, tid, inst);
153 cpu->trapPending[tid] = false;
154 break;
155
156 #if !FULL_SYSTEM
157 case Syscall:
158 cpu->syscall(inst->syscallNum, tid);
159 cpu->resPool->trap(fault, tid, inst);
160 break;
161 #endif
162 default:
163 fatal("Unrecognized Event Type %s", eventNames[cpuEventType]);
164 }
165
166 cpu->cpuEventRemoveList.push(this);
167 }
168
169
170
171 const char *
172 InOrderCPU::CPUEvent::description()
173 {
174 return "InOrderCPU event";
175 }
176
177 void
178 InOrderCPU::CPUEvent::scheduleEvent(int delay)
179 {
180 assert(!scheduled() || squashed());
181 cpu->reschedule(this, cpu->nextCycle(curTick() + cpu->ticks(delay)), true);
182 }
183
184 void
185 InOrderCPU::CPUEvent::unscheduleEvent()
186 {
187 if (scheduled())
188 squash();
189 }
190
191 InOrderCPU::InOrderCPU(Params *params)
192 : BaseCPU(params),
193 cpu_id(params->cpu_id),
194 coreType("default"),
195 _status(Idle),
196 tickEvent(this),
197 stageWidth(params->stageWidth),
198 timeBuffer(2 , 2),
199 removeInstsThisCycle(false),
200 activityRec(params->name, NumStages, 10, params->activity),
201 #if FULL_SYSTEM
202 system(params->system),
203 #endif // FULL_SYSTEM
204 #ifdef DEBUG
205 cpuEventNum(0),
206 resReqCount(0),
207 #endif // DEBUG
208 drainCount(0),
209 deferRegistration(false/*params->deferRegistration*/),
210 stageTracing(params->stageTracing),
211 lastRunningCycle(0),
212 instsPerSwitch(0)
213 {
214 cpu_params = params;
215
216 resPool = new ResourcePool(this, params);
217
218 // Resize for Multithreading CPUs
219 thread.resize(numThreads);
220
221 #if !FULL_SYSTEM
222 ThreadID active_threads = params->workload.size();
223
224 if (active_threads > MaxThreads) {
225 panic("Workload Size too large. Increase the 'MaxThreads'"
226 "in your InOrder implementation or "
227 "edit your workload size.");
228 }
229
230
231 if (active_threads > 1) {
232 threadModel = (InOrderCPU::ThreadModel) params->threadModel;
233
234 if (threadModel == SMT) {
235 DPRINTF(InOrderCPU, "Setting Thread Model to SMT.\n");
236 } else if (threadModel == SwitchOnCacheMiss) {
237 DPRINTF(InOrderCPU, "Setting Thread Model to "
238 "Switch On Cache Miss\n");
239 }
240
241 } else {
242 threadModel = Single;
243 }
244
245
246
247 #endif
248
249 // Bind the fetch & data ports from the resource pool.
250 fetchPortIdx = resPool->getPortIdx(params->fetchMemPort);
251 if (fetchPortIdx == 0) {
252 fatal("Unable to find port to fetch instructions from.\n");
253 }
254
255 dataPortIdx = resPool->getPortIdx(params->dataMemPort);
256 if (dataPortIdx == 0) {
257 fatal("Unable to find port for data.\n");
258 }
259
260 for (ThreadID tid = 0; tid < numThreads; ++tid) {
261 pc[tid].set(0);
262 lastCommittedPC[tid].set(0);
263
264 #if FULL_SYSTEM
265 // SMT is not supported in FS mode yet.
266 assert(numThreads == 1);
267 thread[tid] = new Thread(this, 0);
268 #else
269 if (tid < (ThreadID)params->workload.size()) {
270 DPRINTF(InOrderCPU, "Workload[%i] process is %#x\n",
271 tid, params->workload[tid]->prog_fname);
272 thread[tid] =
273 new Thread(this, tid, params->workload[tid]);
274 } else {
275 //Allocate Empty thread so M5 can use later
276 //when scheduling threads to CPU
277 Process* dummy_proc = params->workload[0];
278 thread[tid] = new Thread(this, tid, dummy_proc);
279 }
280
281 // Eventually set this with parameters...
282 asid[tid] = tid;
283 #endif
284
285 // Setup the TC that will serve as the interface to the threads/CPU.
286 InOrderThreadContext *tc = new InOrderThreadContext;
287 tc->cpu = this;
288 tc->thread = thread[tid];
289
290 #if FULL_SYSTEM
291 // Setup quiesce event.
292 this->thread[tid]->quiesceEvent = new EndQuiesceEvent(tc);
293 #endif
294
295 // Give the thread the TC.
296 thread[tid]->tc = tc;
297 thread[tid]->setFuncExeInst(0);
298 globalSeqNum[tid] = 1;
299
300 // Add the TC to the CPU's list of TC's.
301 this->threadContexts.push_back(tc);
302 }
303
304 // Initialize TimeBuffer Stage Queues
305 for (int stNum=0; stNum < NumStages - 1; stNum++) {
306 stageQueue[stNum] = new StageQueue(NumStages, NumStages);
307 stageQueue[stNum]->id(stNum);
308 }
309
310
311 // Set Up Pipeline Stages
312 for (int stNum=0; stNum < NumStages; stNum++) {
313 if (stNum == 0)
314 pipelineStage[stNum] = new FirstStage(params, stNum);
315 else
316 pipelineStage[stNum] = new PipelineStage(params, stNum);
317
318 pipelineStage[stNum]->setCPU(this);
319 pipelineStage[stNum]->setActiveThreads(&activeThreads);
320 pipelineStage[stNum]->setTimeBuffer(&timeBuffer);
321
322 // Take Care of 1st/Nth stages
323 if (stNum > 0)
324 pipelineStage[stNum]->setPrevStageQueue(stageQueue[stNum - 1]);
325 if (stNum < NumStages - 1)
326 pipelineStage[stNum]->setNextStageQueue(stageQueue[stNum]);
327 }
328
329 // Initialize thread specific variables
330 for (ThreadID tid = 0; tid < numThreads; tid++) {
331 archRegDepMap[tid].setCPU(this);
332
333 nonSpecInstActive[tid] = false;
334 nonSpecSeqNum[tid] = 0;
335
336 squashSeqNum[tid] = MaxAddr;
337 lastSquashCycle[tid] = 0;
338
339 memset(intRegs[tid], 0, sizeof(intRegs[tid]));
340 memset(floatRegs.i[tid], 0, sizeof(floatRegs.i[tid]));
341 isa[tid].clear();
342
343 // Define dummy instructions and resource requests to be used.
344 dummyInst[tid] = new InOrderDynInst(this,
345 thread[tid],
346 0,
347 tid,
348 asid[tid]);
349
350 dummyReq[tid] = new ResourceRequest(resPool->getResource(0));
351
352 #if FULL_SYSTEM
353 // Use this dummy inst to force squashing behind every instruction
354 // in pipeline
355 dummyTrapInst[tid] = new InOrderDynInst(this, NULL, 0, 0, 0);
356 dummyTrapInst[tid]->seqNum = 0;
357 dummyTrapInst[tid]->squashSeqNum = 0;
358 dummyTrapInst[tid]->setTid(tid);
359 #endif
360
361 trapPending[tid] = false;
362
363 }
364
365 dummyReqInst = new InOrderDynInst(this, NULL, 0, 0, 0);
366 dummyReqInst->setSquashed();
367 dummyReqInst->resetInstCount();
368
369 dummyBufferInst = new InOrderDynInst(this, NULL, 0, 0, 0);
370 dummyBufferInst->setSquashed();
371 dummyBufferInst->resetInstCount();
372
373 endOfSkedIt = skedCache.end();
374 frontEndSked = createFrontEndSked();
375 faultSked = createFaultSked();
376
377 lastRunningCycle = curTick();
378
379 lockAddr = 0;
380 lockFlag = false;
381
382 // Schedule First Tick Event, CPU will reschedule itself from here on out.
383 scheduleTickEvent(0);
384 }
385
386 InOrderCPU::~InOrderCPU()
387 {
388 delete resPool;
389
390 SkedCacheIt sked_it = skedCache.begin();
391 SkedCacheIt sked_end = skedCache.end();
392
393 while (sked_it != sked_end) {
394 delete (*sked_it).second;
395 sked_it++;
396 }
397 skedCache.clear();
398 }
399
400 m5::hash_map<InOrderCPU::SkedID, ThePipeline::RSkedPtr> InOrderCPU::skedCache;
401
402 RSkedPtr
403 InOrderCPU::createFrontEndSked()
404 {
405 RSkedPtr res_sked = new ResourceSked();
406 int stage_num = 0;
407 StageScheduler F(res_sked, stage_num++);
408 StageScheduler D(res_sked, stage_num++);
409
410 // FETCH
411 F.needs(FetchSeq, FetchSeqUnit::AssignNextPC);
412 F.needs(ICache, FetchUnit::InitiateFetch);
413
414 // DECODE
415 D.needs(ICache, FetchUnit::CompleteFetch);
416 D.needs(Decode, DecodeUnit::DecodeInst);
417 D.needs(BPred, BranchPredictor::PredictBranch);
418 D.needs(FetchSeq, FetchSeqUnit::UpdateTargetPC);
419
420
421 DPRINTF(SkedCache, "Resource Sked created for instruction Front End\n");
422
423 return res_sked;
424 }
425
426 RSkedPtr
427 InOrderCPU::createFaultSked()
428 {
429 RSkedPtr res_sked = new ResourceSked();
430 StageScheduler W(res_sked, NumStages - 1);
431 W.needs(Grad, GraduationUnit::CheckFault);
432 DPRINTF(SkedCache, "Resource Sked created for instruction Faults\n");
433 return res_sked;
434 }
435
436 RSkedPtr
437 InOrderCPU::createBackEndSked(DynInstPtr inst)
438 {
439 RSkedPtr res_sked = lookupSked(inst);
440 if (res_sked != NULL) {
441 DPRINTF(SkedCache, "Found %s in sked cache.\n",
442 inst->instName());
443 return res_sked;
444 } else {
445 res_sked = new ResourceSked();
446 }
447
448 int stage_num = ThePipeline::BackEndStartStage;
449 StageScheduler X(res_sked, stage_num++);
450 StageScheduler M(res_sked, stage_num++);
451 StageScheduler W(res_sked, stage_num++);
452
453 if (!inst->staticInst) {
454 warn_once("Static Instruction Object Not Set. Can't Create"
455 " Back End Schedule");
456 return NULL;
457 }
458
459 // EXECUTE
460 X.needs(RegManager, UseDefUnit::MarkDestRegs);
461 for (int idx=0; idx < inst->numSrcRegs(); idx++) {
462 if (!idx || !inst->isStore()) {
463 X.needs(RegManager, UseDefUnit::ReadSrcReg, idx);
464 }
465 }
466
467 //@todo: schedule non-spec insts to operate on this cycle
468 // as long as all previous insts are done
469 if ( inst->isNonSpeculative() ) {
470 // skip execution of non speculative insts until later
471 } else if ( inst->isMemRef() ) {
472 if ( inst->isLoad() ) {
473 X.needs(AGEN, AGENUnit::GenerateAddr);
474 }
475 } else if (inst->opClass() == IntMultOp || inst->opClass() == IntDivOp) {
476 X.needs(MDU, MultDivUnit::StartMultDiv);
477 } else {
478 X.needs(ExecUnit, ExecutionUnit::ExecuteInst);
479 }
480
481 // MEMORY
482 if (!inst->isNonSpeculative()) {
483 if (inst->opClass() == IntMultOp || inst->opClass() == IntDivOp) {
484 M.needs(MDU, MultDivUnit::EndMultDiv);
485 }
486
487 if ( inst->isLoad() ) {
488 M.needs(DCache, CacheUnit::InitiateReadData);
489 if (inst->splitInst)
490 M.needs(DCache, CacheUnit::InitSecondSplitRead);
491 } else if ( inst->isStore() ) {
492 for (int i = 1; i < inst->numSrcRegs(); i++ ) {
493 M.needs(RegManager, UseDefUnit::ReadSrcReg, i);
494 }
495 M.needs(AGEN, AGENUnit::GenerateAddr);
496 M.needs(DCache, CacheUnit::InitiateWriteData);
497 if (inst->splitInst)
498 M.needs(DCache, CacheUnit::InitSecondSplitWrite);
499 }
500 }
501
502 // WRITEBACK
503 if (!inst->isNonSpeculative()) {
504 if ( inst->isLoad() ) {
505 W.needs(DCache, CacheUnit::CompleteReadData);
506 if (inst->splitInst)
507 W.needs(DCache, CacheUnit::CompleteSecondSplitRead);
508 } else if ( inst->isStore() ) {
509 W.needs(DCache, CacheUnit::CompleteWriteData);
510 if (inst->splitInst)
511 W.needs(DCache, CacheUnit::CompleteSecondSplitWrite);
512 }
513 } else {
514 // Finally, Execute Speculative Data
515 if (inst->isMemRef()) {
516 if (inst->isLoad()) {
517 W.needs(AGEN, AGENUnit::GenerateAddr);
518 W.needs(DCache, CacheUnit::InitiateReadData);
519 if (inst->splitInst)
520 W.needs(DCache, CacheUnit::InitSecondSplitRead);
521 W.needs(DCache, CacheUnit::CompleteReadData);
522 if (inst->splitInst)
523 W.needs(DCache, CacheUnit::CompleteSecondSplitRead);
524 } else if (inst->isStore()) {
525 if ( inst->numSrcRegs() >= 2 ) {
526 W.needs(RegManager, UseDefUnit::ReadSrcReg, 1);
527 }
528 W.needs(AGEN, AGENUnit::GenerateAddr);
529 W.needs(DCache, CacheUnit::InitiateWriteData);
530 if (inst->splitInst)
531 W.needs(DCache, CacheUnit::InitSecondSplitWrite);
532 W.needs(DCache, CacheUnit::CompleteWriteData);
533 if (inst->splitInst)
534 W.needs(DCache, CacheUnit::CompleteSecondSplitWrite);
535 }
536 } else {
537 W.needs(ExecUnit, ExecutionUnit::ExecuteInst);
538 }
539 }
540
541 W.needs(Grad, GraduationUnit::CheckFault);
542
543 for (int idx=0; idx < inst->numDestRegs(); idx++) {
544 W.needs(RegManager, UseDefUnit::WriteDestReg, idx);
545 }
546
547 if (inst->isControl())
548 W.needs(BPred, BranchPredictor::UpdatePredictor);
549
550 W.needs(Grad, GraduationUnit::GraduateInst);
551
552 // Insert Back Schedule into our cache of
553 // resource schedules
554 addToSkedCache(inst, res_sked);
555
556 DPRINTF(SkedCache, "Back End Sked Created for instruction: %s (%08p)\n",
557 inst->instName(), inst->getMachInst());
558 res_sked->print();
559
560 return res_sked;
561 }
562
563 void
564 InOrderCPU::regStats()
565 {
566 /* Register the Resource Pool's stats here.*/
567 resPool->regStats();
568
569 /* Register for each Pipeline Stage */
570 for (int stage_num=0; stage_num < ThePipeline::NumStages; stage_num++) {
571 pipelineStage[stage_num]->regStats();
572 }
573
574 /* Register any of the InOrderCPU's stats here.*/
575 instsPerCtxtSwitch
576 .name(name() + ".instsPerContextSwitch")
577 .desc("Instructions Committed Per Context Switch")
578 .prereq(instsPerCtxtSwitch);
579
580 numCtxtSwitches
581 .name(name() + ".contextSwitches")
582 .desc("Number of context switches");
583
584 comLoads
585 .name(name() + ".comLoads")
586 .desc("Number of Load instructions committed");
587
588 comStores
589 .name(name() + ".comStores")
590 .desc("Number of Store instructions committed");
591
592 comBranches
593 .name(name() + ".comBranches")
594 .desc("Number of Branches instructions committed");
595
596 comNops
597 .name(name() + ".comNops")
598 .desc("Number of Nop instructions committed");
599
600 comNonSpec
601 .name(name() + ".comNonSpec")
602 .desc("Number of Non-Speculative instructions committed");
603
604 comInts
605 .name(name() + ".comInts")
606 .desc("Number of Integer instructions committed");
607
608 comFloats
609 .name(name() + ".comFloats")
610 .desc("Number of Floating Point instructions committed");
611
612 timesIdled
613 .name(name() + ".timesIdled")
614 .desc("Number of times that the entire CPU went into an idle state and"
615 " unscheduled itself")
616 .prereq(timesIdled);
617
618 idleCycles
619 .name(name() + ".idleCycles")
620 .desc("Number of cycles cpu's stages were not processed");
621
622 runCycles
623 .name(name() + ".runCycles")
624 .desc("Number of cycles cpu stages are processed.");
625
626 activity
627 .name(name() + ".activity")
628 .desc("Percentage of cycles cpu is active")
629 .precision(6);
630 activity = (runCycles / numCycles) * 100;
631
632 threadCycles
633 .init(numThreads)
634 .name(name() + ".threadCycles")
635 .desc("Total Number of Cycles A Thread Was Active in CPU (Per-Thread)");
636
637 smtCycles
638 .name(name() + ".smtCycles")
639 .desc("Total number of cycles that the CPU was in SMT-mode");
640
641 committedInsts
642 .init(numThreads)
643 .name(name() + ".committedInsts")
644 .desc("Number of Instructions Simulated (Per-Thread)");
645
646 smtCommittedInsts
647 .init(numThreads)
648 .name(name() + ".smtCommittedInsts")
649 .desc("Number of SMT Instructions Simulated (Per-Thread)");
650
651 totalCommittedInsts
652 .name(name() + ".committedInsts_total")
653 .desc("Number of Instructions Simulated (Total)");
654
655 cpi
656 .name(name() + ".cpi")
657 .desc("CPI: Cycles Per Instruction (Per-Thread)")
658 .precision(6);
659 cpi = numCycles / committedInsts;
660
661 smtCpi
662 .name(name() + ".smt_cpi")
663 .desc("CPI: Total SMT-CPI")
664 .precision(6);
665 smtCpi = smtCycles / smtCommittedInsts;
666
667 totalCpi
668 .name(name() + ".cpi_total")
669 .desc("CPI: Total CPI of All Threads")
670 .precision(6);
671 totalCpi = numCycles / totalCommittedInsts;
672
673 ipc
674 .name(name() + ".ipc")
675 .desc("IPC: Instructions Per Cycle (Per-Thread)")
676 .precision(6);
677 ipc = committedInsts / numCycles;
678
679 smtIpc
680 .name(name() + ".smt_ipc")
681 .desc("IPC: Total SMT-IPC")
682 .precision(6);
683 smtIpc = smtCommittedInsts / smtCycles;
684
685 totalIpc
686 .name(name() + ".ipc_total")
687 .desc("IPC: Total IPC of All Threads")
688 .precision(6);
689 totalIpc = totalCommittedInsts / numCycles;
690
691 BaseCPU::regStats();
692 }
693
694
695 void
696 InOrderCPU::tick()
697 {
698 DPRINTF(InOrderCPU, "\n\nInOrderCPU: Ticking main, InOrderCPU.\n");
699
700 ++numCycles;
701
702 #if FULL_SYSTEM
703 checkForInterrupts();
704 #endif
705
706 bool pipes_idle = true;
707 //Tick each of the stages
708 for (int stNum=NumStages - 1; stNum >= 0 ; stNum--) {
709 pipelineStage[stNum]->tick();
710
711 pipes_idle = pipes_idle && pipelineStage[stNum]->idle;
712 }
713
714 if (pipes_idle)
715 idleCycles++;
716 else
717 runCycles++;
718
719 // Now advance the time buffers one tick
720 timeBuffer.advance();
721 for (int sqNum=0; sqNum < NumStages - 1; sqNum++) {
722 stageQueue[sqNum]->advance();
723 }
724 activityRec.advance();
725
726 // Any squashed events, or insts then remove them now
727 cleanUpRemovedEvents();
728 cleanUpRemovedInsts();
729
730 // Re-schedule CPU for this cycle
731 if (!tickEvent.scheduled()) {
732 if (_status == SwitchedOut) {
733 // increment stat
734 lastRunningCycle = curTick();
735 } else if (!activityRec.active()) {
736 DPRINTF(InOrderCPU, "sleeping CPU.\n");
737 lastRunningCycle = curTick();
738 timesIdled++;
739 } else {
740 //Tick next_tick = curTick() + cycles(1);
741 //tickEvent.schedule(next_tick);
742 schedule(&tickEvent, nextCycle(curTick() + 1));
743 DPRINTF(InOrderCPU, "Scheduled CPU for next tick @ %i.\n",
744 nextCycle(curTick() + 1));
745 }
746 }
747
748 tickThreadStats();
749 updateThreadPriority();
750 }
751
752
753 void
754 InOrderCPU::init()
755 {
756 if (!deferRegistration) {
757 registerThreadContexts();
758 }
759
760 // Set inSyscall so that the CPU doesn't squash when initially
761 // setting up registers.
762 for (ThreadID tid = 0; tid < numThreads; ++tid)
763 thread[tid]->inSyscall = true;
764
765 #if FULL_SYSTEM
766 for (ThreadID tid = 0; tid < numThreads; tid++) {
767 ThreadContext *src_tc = threadContexts[tid];
768 TheISA::initCPU(src_tc, src_tc->contextId());
769 }
770 #endif
771
772 // Clear inSyscall.
773 for (ThreadID tid = 0; tid < numThreads; ++tid)
774 thread[tid]->inSyscall = false;
775
776 // Call Initializiation Routine for Resource Pool
777 resPool->init();
778 }
779
780 Port*
781 InOrderCPU::getPort(const std::string &if_name, int idx)
782 {
783 return resPool->getPort(if_name, idx);
784 }
785
786 #if FULL_SYSTEM
787 Fault
788 InOrderCPU::hwrei(ThreadID tid)
789 {
790 #if THE_ISA == ALPHA_ISA
791 // Need to clear the lock flag upon returning from an interrupt.
792 setMiscRegNoEffect(AlphaISA::MISCREG_LOCKFLAG, false, tid);
793
794 thread[tid]->kernelStats->hwrei();
795 // FIXME: XXX check for interrupts? XXX
796 #endif
797
798 return NoFault;
799 }
800
801
802 bool
803 InOrderCPU::simPalCheck(int palFunc, ThreadID tid)
804 {
805 #if THE_ISA == ALPHA_ISA
806 if (this->thread[tid]->kernelStats)
807 this->thread[tid]->kernelStats->callpal(palFunc,
808 this->threadContexts[tid]);
809
810 switch (palFunc) {
811 case PAL::halt:
812 halt();
813 if (--System::numSystemsRunning == 0)
814 exitSimLoop("all cpus halted");
815 break;
816
817 case PAL::bpt:
818 case PAL::bugchk:
819 if (this->system->breakpoint())
820 return false;
821 break;
822 }
823 #endif
824 return true;
825 }
826
827 void
828 InOrderCPU::checkForInterrupts()
829 {
830 for (int i = 0; i < threadContexts.size(); i++) {
831 ThreadContext *tc = threadContexts[i];
832
833 if (interrupts->checkInterrupts(tc)) {
834 Fault interrupt = interrupts->getInterrupt(tc);
835
836 if (interrupt != NoFault) {
837 DPRINTF(Interrupt, "Processing Intterupt for [tid:%i].\n",
838 tc->threadId());
839
840 ThreadID tid = tc->threadId();
841 interrupts->updateIntrInfo(tc);
842
843 // Squash from Last Stage in Pipeline
844 unsigned last_stage = NumStages - 1;
845 dummyTrapInst[tid]->squashingStage = last_stage;
846 pipelineStage[last_stage]->setupSquash(dummyTrapInst[tid],
847 tid);
848
849 // By default, setupSquash will always squash from stage + 1
850 pipelineStage[BackEndStartStage - 1]->setupSquash(dummyTrapInst[tid],
851 tid);
852
853 // Schedule Squash Through-out Resource Pool
854 resPool->scheduleEvent(
855 (InOrderCPU::CPUEventType)ResourcePool::SquashAll,
856 dummyTrapInst[tid], 0);
857
858 // Finally, Setup Trap to happen at end of cycle
859 trapContext(interrupt, tid, dummyTrapInst[tid]);
860 }
861 }
862 }
863 }
864
865 Fault
866 InOrderCPU::getInterrupts()
867 {
868 // Check if there are any outstanding interrupts
869 return interrupts->getInterrupt(threadContexts[0]);
870 }
871
872
873 void
874 InOrderCPU::processInterrupts(Fault interrupt)
875 {
876 // Check for interrupts here. For now can copy the code that
877 // exists within isa_fullsys_traits.hh. Also assume that thread 0
878 // is the one that handles the interrupts.
879 // @todo: Possibly consolidate the interrupt checking code.
880 // @todo: Allow other threads to handle interrupts.
881
882 assert(interrupt != NoFault);
883 interrupts->updateIntrInfo(threadContexts[0]);
884
885 DPRINTF(InOrderCPU, "Interrupt %s being handled\n", interrupt->name());
886
887 // Note: Context ID ok here? Impl. of FS mode needs to revisit this
888 trap(interrupt, threadContexts[0]->contextId(), dummyBufferInst);
889 }
890
891
892 void
893 InOrderCPU::updateMemPorts()
894 {
895 // Update all ThreadContext's memory ports (Functional/Virtual
896 // Ports)
897 ThreadID size = thread.size();
898 for (ThreadID i = 0; i < size; ++i)
899 thread[i]->connectMemPorts(thread[i]->getTC());
900 }
901 #endif
902
903 void
904 InOrderCPU::trapContext(Fault fault, ThreadID tid, DynInstPtr inst, int delay)
905 {
906 scheduleCpuEvent(Trap, fault, tid, inst, delay);
907 trapPending[tid] = true;
908 }
909
910 void
911 InOrderCPU::trap(Fault fault, ThreadID tid, DynInstPtr inst)
912 {
913 fault->invoke(tcBase(tid), inst->staticInst);
914 removePipelineStalls(tid);
915 }
916
917 void
918 InOrderCPU::squashFromMemStall(DynInstPtr inst, ThreadID tid, int delay)
919 {
920 scheduleCpuEvent(SquashFromMemStall, NoFault, tid, inst, delay);
921 }
922
923
924 void
925 InOrderCPU::squashDueToMemStall(int stage_num, InstSeqNum seq_num,
926 ThreadID tid)
927 {
928 DPRINTF(InOrderCPU, "Squashing Pipeline Stages Due to Memory Stall...\n");
929
930 // Squash all instructions in each stage including
931 // instruction that caused the squash (seq_num - 1)
932 // NOTE: The stage bandwidth needs to be cleared so thats why
933 // the stalling instruction is squashed as well. The stalled
934 // instruction is previously placed in another intermediate buffer
935 // while it's stall is being handled.
936 InstSeqNum squash_seq_num = seq_num - 1;
937
938 for (int stNum=stage_num; stNum >= 0 ; stNum--) {
939 pipelineStage[stNum]->squashDueToMemStall(squash_seq_num, tid);
940 }
941 }
942
943 void
944 InOrderCPU::scheduleCpuEvent(CPUEventType c_event, Fault fault,
945 ThreadID tid, DynInstPtr inst,
946 unsigned delay, CPUEventPri event_pri)
947 {
948 CPUEvent *cpu_event = new CPUEvent(this, c_event, fault, tid, inst,
949 event_pri);
950
951 Tick sked_tick = nextCycle(curTick() + ticks(delay));
952 if (delay >= 0) {
953 DPRINTF(InOrderCPU, "Scheduling CPU Event (%s) for cycle %i, [tid:%i].\n",
954 eventNames[c_event], curTick() + delay, tid);
955 schedule(cpu_event, sked_tick);
956 } else {
957 cpu_event->process();
958 cpuEventRemoveList.push(cpu_event);
959 }
960
961 // Broadcast event to the Resource Pool
962 // Need to reset tid just in case this is a dummy instruction
963 inst->setTid(tid);
964 resPool->scheduleEvent(c_event, inst, 0, 0, tid);
965 }
966
967 bool
968 InOrderCPU::isThreadActive(ThreadID tid)
969 {
970 list<ThreadID>::iterator isActive =
971 std::find(activeThreads.begin(), activeThreads.end(), tid);
972
973 return (isActive != activeThreads.end());
974 }
975
976 bool
977 InOrderCPU::isThreadReady(ThreadID tid)
978 {
979 list<ThreadID>::iterator isReady =
980 std::find(readyThreads.begin(), readyThreads.end(), tid);
981
982 return (isReady != readyThreads.end());
983 }
984
985 bool
986 InOrderCPU::isThreadSuspended(ThreadID tid)
987 {
988 list<ThreadID>::iterator isSuspended =
989 std::find(suspendedThreads.begin(), suspendedThreads.end(), tid);
990
991 return (isSuspended != suspendedThreads.end());
992 }
993
994 void
995 InOrderCPU::activateNextReadyThread()
996 {
997 if (readyThreads.size() >= 1) {
998 ThreadID ready_tid = readyThreads.front();
999
1000 // Activate in Pipeline
1001 activateThread(ready_tid);
1002
1003 // Activate in Resource Pool
1004 resPool->activateThread(ready_tid);
1005
1006 list<ThreadID>::iterator ready_it =
1007 std::find(readyThreads.begin(), readyThreads.end(), ready_tid);
1008 readyThreads.erase(ready_it);
1009 } else {
1010 DPRINTF(InOrderCPU,
1011 "Attempting to activate new thread, but No Ready Threads to"
1012 "activate.\n");
1013 DPRINTF(InOrderCPU,
1014 "Unable to switch to next active thread.\n");
1015 }
1016 }
1017
1018 void
1019 InOrderCPU::activateThread(ThreadID tid)
1020 {
1021 if (isThreadSuspended(tid)) {
1022 DPRINTF(InOrderCPU,
1023 "Removing [tid:%i] from suspended threads list.\n", tid);
1024
1025 list<ThreadID>::iterator susp_it =
1026 std::find(suspendedThreads.begin(), suspendedThreads.end(),
1027 tid);
1028 suspendedThreads.erase(susp_it);
1029 }
1030
1031 if (threadModel == SwitchOnCacheMiss &&
1032 numActiveThreads() == 1) {
1033 DPRINTF(InOrderCPU,
1034 "Ignoring activation of [tid:%i], since [tid:%i] is "
1035 "already running.\n", tid, activeThreadId());
1036
1037 DPRINTF(InOrderCPU,"Placing [tid:%i] on ready threads list\n",
1038 tid);
1039
1040 readyThreads.push_back(tid);
1041
1042 } else if (!isThreadActive(tid)) {
1043 DPRINTF(InOrderCPU,
1044 "Adding [tid:%i] to active threads list.\n", tid);
1045 activeThreads.push_back(tid);
1046
1047 activateThreadInPipeline(tid);
1048
1049 thread[tid]->lastActivate = curTick();
1050
1051 tcBase(tid)->setStatus(ThreadContext::Active);
1052
1053 wakeCPU();
1054
1055 numCtxtSwitches++;
1056 }
1057 }
1058
1059 void
1060 InOrderCPU::activateThreadInPipeline(ThreadID tid)
1061 {
1062 for (int stNum=0; stNum < NumStages; stNum++) {
1063 pipelineStage[stNum]->activateThread(tid);
1064 }
1065 }
1066
1067 void
1068 InOrderCPU::deactivateContext(ThreadID tid, int delay)
1069 {
1070 DPRINTF(InOrderCPU,"[tid:%i]: Deactivating ...\n", tid);
1071
1072 scheduleCpuEvent(DeactivateThread, NoFault, tid, dummyInst[tid], delay);
1073
1074 // Be sure to signal that there's some activity so the CPU doesn't
1075 // deschedule itself.
1076 activityRec.activity();
1077
1078 _status = Running;
1079 }
1080
1081 void
1082 InOrderCPU::deactivateThread(ThreadID tid)
1083 {
1084 DPRINTF(InOrderCPU, "[tid:%i]: Calling deactivate thread.\n", tid);
1085
1086 if (isThreadActive(tid)) {
1087 DPRINTF(InOrderCPU,"[tid:%i]: Removing from active threads list\n",
1088 tid);
1089 list<ThreadID>::iterator thread_it =
1090 std::find(activeThreads.begin(), activeThreads.end(), tid);
1091
1092 removePipelineStalls(*thread_it);
1093
1094 activeThreads.erase(thread_it);
1095
1096 // Ideally, this should be triggered from the
1097 // suspendContext/Thread functions
1098 tcBase(tid)->setStatus(ThreadContext::Suspended);
1099 }
1100
1101 assert(!isThreadActive(tid));
1102 }
1103
1104 void
1105 InOrderCPU::removePipelineStalls(ThreadID tid)
1106 {
1107 DPRINTF(InOrderCPU,"[tid:%i]: Removing all pipeline stalls\n",
1108 tid);
1109
1110 for (int stNum = 0; stNum < NumStages ; stNum++) {
1111 pipelineStage[stNum]->removeStalls(tid);
1112 }
1113
1114 }
1115
1116 void
1117 InOrderCPU::updateThreadPriority()
1118 {
1119 if (activeThreads.size() > 1)
1120 {
1121 //DEFAULT TO ROUND ROBIN SCHEME
1122 //e.g. Move highest priority to end of thread list
1123 list<ThreadID>::iterator list_begin = activeThreads.begin();
1124
1125 unsigned high_thread = *list_begin;
1126
1127 activeThreads.erase(list_begin);
1128
1129 activeThreads.push_back(high_thread);
1130 }
1131 }
1132
1133 inline void
1134 InOrderCPU::tickThreadStats()
1135 {
1136 /** Keep track of cycles that each thread is active */
1137 list<ThreadID>::iterator thread_it = activeThreads.begin();
1138 while (thread_it != activeThreads.end()) {
1139 threadCycles[*thread_it]++;
1140 thread_it++;
1141 }
1142
1143 // Keep track of cycles where SMT is active
1144 if (activeThreads.size() > 1) {
1145 smtCycles++;
1146 }
1147 }
1148
1149 void
1150 InOrderCPU::activateContext(ThreadID tid, int delay)
1151 {
1152 DPRINTF(InOrderCPU,"[tid:%i]: Activating ...\n", tid);
1153
1154
1155 scheduleCpuEvent(ActivateThread, NoFault, tid, dummyInst[tid], delay);
1156
1157 // Be sure to signal that there's some activity so the CPU doesn't
1158 // deschedule itself.
1159 activityRec.activity();
1160
1161 _status = Running;
1162 }
1163
1164 void
1165 InOrderCPU::activateNextReadyContext(int delay)
1166 {
1167 DPRINTF(InOrderCPU,"Activating next ready thread\n");
1168
1169 scheduleCpuEvent(ActivateNextReadyThread, NoFault, 0/*tid*/, dummyInst[0],
1170 delay, ActivateNextReadyThread_Pri);
1171
1172 // Be sure to signal that there's some activity so the CPU doesn't
1173 // deschedule itself.
1174 activityRec.activity();
1175
1176 _status = Running;
1177 }
1178
1179 void
1180 InOrderCPU::haltContext(ThreadID tid, int delay)
1181 {
1182 DPRINTF(InOrderCPU, "[tid:%i]: Calling Halt Context...\n", tid);
1183
1184 scheduleCpuEvent(HaltThread, NoFault, tid, dummyInst[tid], delay);
1185
1186 activityRec.activity();
1187 }
1188
1189 void
1190 InOrderCPU::haltThread(ThreadID tid)
1191 {
1192 DPRINTF(InOrderCPU, "[tid:%i]: Placing on Halted Threads List...\n", tid);
1193 deactivateThread(tid);
1194 squashThreadInPipeline(tid);
1195 haltedThreads.push_back(tid);
1196
1197 tcBase(tid)->setStatus(ThreadContext::Halted);
1198
1199 if (threadModel == SwitchOnCacheMiss) {
1200 activateNextReadyContext();
1201 }
1202 }
1203
1204 void
1205 InOrderCPU::suspendContext(ThreadID tid, int delay)
1206 {
1207 scheduleCpuEvent(SuspendThread, NoFault, tid, dummyInst[tid], delay);
1208 }
1209
1210 void
1211 InOrderCPU::suspendThread(ThreadID tid)
1212 {
1213 DPRINTF(InOrderCPU, "[tid:%i]: Placing on Suspended Threads List...\n",
1214 tid);
1215 deactivateThread(tid);
1216 suspendedThreads.push_back(tid);
1217 thread[tid]->lastSuspend = curTick();
1218
1219 tcBase(tid)->setStatus(ThreadContext::Suspended);
1220 }
1221
1222 void
1223 InOrderCPU::squashThreadInPipeline(ThreadID tid)
1224 {
1225 //Squash all instructions in each stage
1226 for (int stNum=NumStages - 1; stNum >= 0 ; stNum--) {
1227 pipelineStage[stNum]->squash(0 /*seq_num*/, tid);
1228 }
1229 }
1230
1231 PipelineStage*
1232 InOrderCPU::getPipeStage(int stage_num)
1233 {
1234 return pipelineStage[stage_num];
1235 }
1236
1237
1238 RegIndex
1239 InOrderCPU::flattenRegIdx(RegIndex reg_idx, RegType &reg_type, ThreadID tid)
1240 {
1241 if (reg_idx < FP_Base_DepTag) {
1242 reg_type = IntType;
1243 return isa[tid].flattenIntIndex(reg_idx);
1244 } else if (reg_idx < Ctrl_Base_DepTag) {
1245 reg_type = FloatType;
1246 reg_idx -= FP_Base_DepTag;
1247 return isa[tid].flattenFloatIndex(reg_idx);
1248 } else {
1249 reg_type = MiscType;
1250 return reg_idx - TheISA::Ctrl_Base_DepTag;
1251 }
1252 }
1253
1254 uint64_t
1255 InOrderCPU::readIntReg(RegIndex reg_idx, ThreadID tid)
1256 {
1257 DPRINTF(IntRegs, "[tid:%i]: Reading Int. Reg %i as %x\n",
1258 tid, reg_idx, intRegs[tid][reg_idx]);
1259
1260 return intRegs[tid][reg_idx];
1261 }
1262
1263 FloatReg
1264 InOrderCPU::readFloatReg(RegIndex reg_idx, ThreadID tid)
1265 {
1266 DPRINTF(FloatRegs, "[tid:%i]: Reading Float Reg %i as %x, %08f\n",
1267 tid, reg_idx, floatRegs.i[tid][reg_idx], floatRegs.f[tid][reg_idx]);
1268
1269 return floatRegs.f[tid][reg_idx];
1270 }
1271
1272 FloatRegBits
1273 InOrderCPU::readFloatRegBits(RegIndex reg_idx, ThreadID tid)
1274 {
1275 DPRINTF(FloatRegs, "[tid:%i]: Reading Float Reg %i as %x, %08f\n",
1276 tid, reg_idx, floatRegs.i[tid][reg_idx], floatRegs.f[tid][reg_idx]);
1277
1278 return floatRegs.i[tid][reg_idx];
1279 }
1280
1281 void
1282 InOrderCPU::setIntReg(RegIndex reg_idx, uint64_t val, ThreadID tid)
1283 {
1284 if (reg_idx == TheISA::ZeroReg) {
1285 DPRINTF(IntRegs, "[tid:%i]: Ignoring Setting of ISA-ZeroReg "
1286 "(Int. Reg %i) to %x\n", tid, reg_idx, val);
1287 return;
1288 } else {
1289 DPRINTF(IntRegs, "[tid:%i]: Setting Int. Reg %i to %x\n",
1290 tid, reg_idx, val);
1291
1292 intRegs[tid][reg_idx] = val;
1293 }
1294 }
1295
1296
1297 void
1298 InOrderCPU::setFloatReg(RegIndex reg_idx, FloatReg val, ThreadID tid)
1299 {
1300 floatRegs.f[tid][reg_idx] = val;
1301 DPRINTF(FloatRegs, "[tid:%i]: Setting Float. Reg %i bits to "
1302 "%x, %08f\n",
1303 tid, reg_idx,
1304 floatRegs.i[tid][reg_idx],
1305 floatRegs.f[tid][reg_idx]);
1306 }
1307
1308
1309 void
1310 InOrderCPU::setFloatRegBits(RegIndex reg_idx, FloatRegBits val, ThreadID tid)
1311 {
1312 floatRegs.i[tid][reg_idx] = val;
1313 DPRINTF(FloatRegs, "[tid:%i]: Setting Float. Reg %i bits to "
1314 "%x, %08f\n",
1315 tid, reg_idx,
1316 floatRegs.i[tid][reg_idx],
1317 floatRegs.f[tid][reg_idx]);
1318 }
1319
1320 uint64_t
1321 InOrderCPU::readRegOtherThread(unsigned reg_idx, ThreadID tid)
1322 {
1323 // If Default value is set, then retrieve target thread
1324 if (tid == InvalidThreadID) {
1325 tid = TheISA::getTargetThread(tcBase(tid));
1326 }
1327
1328 if (reg_idx < FP_Base_DepTag) {
1329 // Integer Register File
1330 return readIntReg(reg_idx, tid);
1331 } else if (reg_idx < Ctrl_Base_DepTag) {
1332 // Float Register File
1333 reg_idx -= FP_Base_DepTag;
1334 return readFloatRegBits(reg_idx, tid);
1335 } else {
1336 reg_idx -= Ctrl_Base_DepTag;
1337 return readMiscReg(reg_idx, tid); // Misc. Register File
1338 }
1339 }
1340 void
1341 InOrderCPU::setRegOtherThread(unsigned reg_idx, const MiscReg &val,
1342 ThreadID tid)
1343 {
1344 // If Default value is set, then retrieve target thread
1345 if (tid == InvalidThreadID) {
1346 tid = TheISA::getTargetThread(tcBase(tid));
1347 }
1348
1349 if (reg_idx < FP_Base_DepTag) { // Integer Register File
1350 setIntReg(reg_idx, val, tid);
1351 } else if (reg_idx < Ctrl_Base_DepTag) { // Float Register File
1352 reg_idx -= FP_Base_DepTag;
1353 setFloatRegBits(reg_idx, val, tid);
1354 } else {
1355 reg_idx -= Ctrl_Base_DepTag;
1356 setMiscReg(reg_idx, val, tid); // Misc. Register File
1357 }
1358 }
1359
1360 MiscReg
1361 InOrderCPU::readMiscRegNoEffect(int misc_reg, ThreadID tid)
1362 {
1363 return isa[tid].readMiscRegNoEffect(misc_reg);
1364 }
1365
1366 MiscReg
1367 InOrderCPU::readMiscReg(int misc_reg, ThreadID tid)
1368 {
1369 return isa[tid].readMiscReg(misc_reg, tcBase(tid));
1370 }
1371
1372 void
1373 InOrderCPU::setMiscRegNoEffect(int misc_reg, const MiscReg &val, ThreadID tid)
1374 {
1375 isa[tid].setMiscRegNoEffect(misc_reg, val);
1376 }
1377
1378 void
1379 InOrderCPU::setMiscReg(int misc_reg, const MiscReg &val, ThreadID tid)
1380 {
1381 isa[tid].setMiscReg(misc_reg, val, tcBase(tid));
1382 }
1383
1384
1385 InOrderCPU::ListIt
1386 InOrderCPU::addInst(DynInstPtr inst)
1387 {
1388 ThreadID tid = inst->readTid();
1389
1390 instList[tid].push_back(inst);
1391
1392 return --(instList[tid].end());
1393 }
1394
1395 InOrderCPU::ListIt
1396 InOrderCPU::findInst(InstSeqNum seq_num, ThreadID tid)
1397 {
1398 ListIt it = instList[tid].begin();
1399 ListIt end = instList[tid].end();
1400
1401 while (it != end) {
1402 if ((*it)->seqNum == seq_num)
1403 return it;
1404 else if ((*it)->seqNum > seq_num)
1405 break;
1406
1407 it++;
1408 }
1409
1410 return instList[tid].end();
1411 }
1412
1413 void
1414 InOrderCPU::updateContextSwitchStats()
1415 {
1416 // Set Average Stat Here, then reset to 0
1417 instsPerCtxtSwitch = instsPerSwitch;
1418 instsPerSwitch = 0;
1419 }
1420
1421
1422 void
1423 InOrderCPU::instDone(DynInstPtr inst, ThreadID tid)
1424 {
1425 // Set the nextPC to be fetched if this is the last instruction
1426 // committed
1427 // ========
1428 // This contributes to the precise state of the CPU
1429 // which can be used when restoring a thread to the CPU after after any
1430 // type of context switching activity (fork, exception, etc.)
1431 TheISA::PCState comm_pc = inst->pcState();
1432 lastCommittedPC[tid] = comm_pc;
1433 TheISA::advancePC(comm_pc, inst->staticInst);
1434 pcState(comm_pc, tid);
1435
1436 //@todo: may be unnecessary with new-ISA-specific branch handling code
1437 if (inst->isControl()) {
1438 thread[tid]->lastGradIsBranch = true;
1439 thread[tid]->lastBranchPC = inst->pcState();
1440 TheISA::advancePC(thread[tid]->lastBranchPC, inst->staticInst);
1441 } else {
1442 thread[tid]->lastGradIsBranch = false;
1443 }
1444
1445
1446 // Finalize Trace Data For Instruction
1447 if (inst->traceData) {
1448 //inst->traceData->setCycle(curTick());
1449 inst->traceData->setFetchSeq(inst->seqNum);
1450 //inst->traceData->setCPSeq(cpu->tcBase(tid)->numInst);
1451 inst->traceData->dump();
1452 delete inst->traceData;
1453 inst->traceData = NULL;
1454 }
1455
1456 // Increment active thread's instruction count
1457 instsPerSwitch++;
1458
1459 // Increment thread-state's instruction count
1460 thread[tid]->numInst++;
1461
1462 // Increment thread-state's instruction stats
1463 thread[tid]->numInsts++;
1464
1465 // Count committed insts per thread stats
1466 committedInsts[tid]++;
1467
1468 // Count total insts committed stat
1469 totalCommittedInsts++;
1470
1471 // Count SMT-committed insts per thread stat
1472 if (numActiveThreads() > 1) {
1473 smtCommittedInsts[tid]++;
1474 }
1475
1476 // Instruction-Mix Stats
1477 if (inst->isLoad()) {
1478 comLoads++;
1479 } else if (inst->isStore()) {
1480 comStores++;
1481 } else if (inst->isControl()) {
1482 comBranches++;
1483 } else if (inst->isNop()) {
1484 comNops++;
1485 } else if (inst->isNonSpeculative()) {
1486 comNonSpec++;
1487 } else if (inst->isInteger()) {
1488 comInts++;
1489 } else if (inst->isFloating()) {
1490 comFloats++;
1491 }
1492
1493 // Check for instruction-count-based events.
1494 comInstEventQueue[tid]->serviceEvents(thread[tid]->numInst);
1495
1496 // Finally, remove instruction from CPU
1497 removeInst(inst);
1498 }
1499
1500 // currently unused function, but substitute repetitive code w/this function
1501 // call
1502 void
1503 InOrderCPU::addToRemoveList(DynInstPtr inst)
1504 {
1505 removeInstsThisCycle = true;
1506 if (!inst->isRemoveList()) {
1507 DPRINTF(InOrderCPU, "Pushing instruction [tid:%i] PC %s "
1508 "[sn:%lli] to remove list\n",
1509 inst->threadNumber, inst->pcState(), inst->seqNum);
1510 inst->setRemoveList();
1511 removeList.push(inst->getInstListIt());
1512 } else {
1513 DPRINTF(InOrderCPU, "Ignoring instruction removal for [tid:%i] PC %s "
1514 "[sn:%lli], already remove list\n",
1515 inst->threadNumber, inst->pcState(), inst->seqNum);
1516 }
1517
1518 }
1519
1520 void
1521 InOrderCPU::removeInst(DynInstPtr inst)
1522 {
1523 DPRINTF(InOrderCPU, "Removing graduated instruction [tid:%i] PC %s "
1524 "[sn:%lli]\n",
1525 inst->threadNumber, inst->pcState(), inst->seqNum);
1526
1527 removeInstsThisCycle = true;
1528
1529 // Remove the instruction.
1530 if (!inst->isRemoveList()) {
1531 DPRINTF(InOrderCPU, "Pushing instruction [tid:%i] PC %s "
1532 "[sn:%lli] to remove list\n",
1533 inst->threadNumber, inst->pcState(), inst->seqNum);
1534 inst->setRemoveList();
1535 removeList.push(inst->getInstListIt());
1536 } else {
1537 DPRINTF(InOrderCPU, "Ignoring instruction removal for [tid:%i] PC %s "
1538 "[sn:%lli], already on remove list\n",
1539 inst->threadNumber, inst->pcState(), inst->seqNum);
1540 }
1541
1542 }
1543
1544 void
1545 InOrderCPU::removeInstsUntil(const InstSeqNum &seq_num, ThreadID tid)
1546 {
1547 //assert(!instList[tid].empty());
1548
1549 removeInstsThisCycle = true;
1550
1551 ListIt inst_iter = instList[tid].end();
1552
1553 inst_iter--;
1554
1555 DPRINTF(InOrderCPU, "Squashing instructions from CPU instruction "
1556 "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
1557 tid, seq_num, (*inst_iter)->seqNum);
1558
1559 while ((*inst_iter)->seqNum > seq_num) {
1560
1561 bool break_loop = (inst_iter == instList[tid].begin());
1562
1563 squashInstIt(inst_iter, tid);
1564
1565 inst_iter--;
1566
1567 if (break_loop)
1568 break;
1569 }
1570 }
1571
1572
1573 inline void
1574 InOrderCPU::squashInstIt(const ListIt inst_it, ThreadID tid)
1575 {
1576 DynInstPtr inst = (*inst_it);
1577 if (inst->threadNumber == tid) {
1578 DPRINTF(InOrderCPU, "Squashing instruction, "
1579 "[tid:%i] [sn:%lli] PC %s\n",
1580 inst->threadNumber,
1581 inst->seqNum,
1582 inst->pcState());
1583
1584 inst->setSquashed();
1585 archRegDepMap[tid].remove(inst);
1586
1587 if (!inst->isRemoveList()) {
1588 DPRINTF(InOrderCPU, "Pushing instruction [tid:%i] PC %s "
1589 "[sn:%lli] to remove list\n",
1590 inst->threadNumber, inst->pcState(),
1591 inst->seqNum);
1592 inst->setRemoveList();
1593 removeList.push(inst_it);
1594 } else {
1595 DPRINTF(InOrderCPU, "Ignoring instruction removal for [tid:%i]"
1596 " PC %s [sn:%lli], already on remove list\n",
1597 inst->threadNumber, inst->pcState(),
1598 inst->seqNum);
1599 }
1600
1601 }
1602
1603 }
1604
1605
1606 void
1607 InOrderCPU::cleanUpRemovedInsts()
1608 {
1609 while (!removeList.empty()) {
1610 DPRINTF(InOrderCPU, "Removing instruction, "
1611 "[tid:%i] [sn:%lli] PC %s\n",
1612 (*removeList.front())->threadNumber,
1613 (*removeList.front())->seqNum,
1614 (*removeList.front())->pcState());
1615
1616 DynInstPtr inst = *removeList.front();
1617 ThreadID tid = inst->threadNumber;
1618
1619 // Remove From Register Dependency Map, If Necessary
1620 // archRegDepMap[tid].remove(inst);
1621
1622 // Clear if Non-Speculative
1623 if (inst->staticInst &&
1624 inst->seqNum == nonSpecSeqNum[tid] &&
1625 nonSpecInstActive[tid] == true) {
1626 nonSpecInstActive[tid] = false;
1627 }
1628
1629 inst->onInstList = false;
1630
1631 instList[tid].erase(removeList.front());
1632
1633 removeList.pop();
1634 }
1635
1636 removeInstsThisCycle = false;
1637 }
1638
1639 void
1640 InOrderCPU::cleanUpRemovedEvents()
1641 {
1642 while (!cpuEventRemoveList.empty()) {
1643 Event *cpu_event = cpuEventRemoveList.front();
1644 cpuEventRemoveList.pop();
1645 delete cpu_event;
1646 }
1647 }
1648
1649
1650 void
1651 InOrderCPU::dumpInsts()
1652 {
1653 int num = 0;
1654
1655 ListIt inst_list_it = instList[0].begin();
1656
1657 cprintf("Dumping Instruction List\n");
1658
1659 while (inst_list_it != instList[0].end()) {
1660 cprintf("Instruction:%i\nPC:%s\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
1661 "Squashed:%i\n\n",
1662 num, (*inst_list_it)->pcState(),
1663 (*inst_list_it)->threadNumber,
1664 (*inst_list_it)->seqNum, (*inst_list_it)->isIssued(),
1665 (*inst_list_it)->isSquashed());
1666 inst_list_it++;
1667 ++num;
1668 }
1669 }
1670
1671 void
1672 InOrderCPU::wakeCPU()
1673 {
1674 if (/*activityRec.active() || */tickEvent.scheduled()) {
1675 DPRINTF(Activity, "CPU already running.\n");
1676 return;
1677 }
1678
1679 DPRINTF(Activity, "Waking up CPU\n");
1680
1681 Tick extra_cycles = tickToCycles((curTick() - 1) - lastRunningCycle);
1682
1683 idleCycles += extra_cycles;
1684 for (int stage_num = 0; stage_num < NumStages; stage_num++) {
1685 pipelineStage[stage_num]->idleCycles += extra_cycles;
1686 }
1687
1688 numCycles += extra_cycles;
1689
1690 schedule(&tickEvent, nextCycle(curTick()));
1691 }
1692
1693 #if FULL_SYSTEM
1694 // Lots of copied full system code...place into BaseCPU class?
1695 void
1696 InOrderCPU::wakeup()
1697 {
1698 if (thread[0]->status() != ThreadContext::Suspended)
1699 return;
1700
1701 wakeCPU();
1702
1703 DPRINTF(Quiesce, "Suspended Processor woken\n");
1704 threadContexts[0]->activate();
1705 }
1706 #endif
1707
1708 #if !FULL_SYSTEM
1709 void
1710 InOrderCPU::syscallContext(Fault fault, ThreadID tid, DynInstPtr inst, int delay)
1711 {
1712 // Syscall must be non-speculative, so squash from last stage
1713 unsigned squash_stage = NumStages - 1;
1714 inst->setSquashInfo(squash_stage);
1715
1716 // Squash In Pipeline Stage
1717 pipelineStage[squash_stage]->setupSquash(inst, tid);
1718
1719 // Schedule Squash Through-out Resource Pool
1720 resPool->scheduleEvent(
1721 (InOrderCPU::CPUEventType)ResourcePool::SquashAll, inst, 0);
1722 scheduleCpuEvent(Syscall, fault, tid, inst, delay, Syscall_Pri);
1723 }
1724
1725 void
1726 InOrderCPU::syscall(int64_t callnum, ThreadID tid)
1727 {
1728 DPRINTF(InOrderCPU, "[tid:%i] Executing syscall().\n\n", tid);
1729
1730 DPRINTF(Activity,"Activity: syscall() called.\n");
1731
1732 // Temporarily increase this by one to account for the syscall
1733 // instruction.
1734 ++(this->thread[tid]->funcExeInst);
1735
1736 // Execute the actual syscall.
1737 this->thread[tid]->syscall(callnum);
1738
1739 // Decrease funcExeInst by one as the normal commit will handle
1740 // incrementing it.
1741 --(this->thread[tid]->funcExeInst);
1742
1743 // Clear Non-Speculative Block Variable
1744 nonSpecInstActive[tid] = false;
1745 }
1746 #endif
1747
1748 TheISA::TLB*
1749 InOrderCPU::getITBPtr()
1750 {
1751 CacheUnit *itb_res =
1752 dynamic_cast<CacheUnit*>(resPool->getResource(fetchPortIdx));
1753 return itb_res->tlb();
1754 }
1755
1756
1757 TheISA::TLB*
1758 InOrderCPU::getDTBPtr()
1759 {
1760 CacheUnit *dtb_res =
1761 dynamic_cast<CacheUnit*>(resPool->getResource(dataPortIdx));
1762 return dtb_res->tlb();
1763 }
1764
1765 Decoder *
1766 InOrderCPU::getDecoderPtr()
1767 {
1768 FetchUnit *fetch_res =
1769 dynamic_cast<FetchUnit*>(resPool->getResource(fetchPortIdx));
1770 return &fetch_res->decoder;
1771 }
1772
1773 Fault
1774 InOrderCPU::read(DynInstPtr inst, Addr addr,
1775 uint8_t *data, unsigned size, unsigned flags)
1776 {
1777 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1778 // you want to run w/out caches?
1779 CacheUnit *cache_res =
1780 dynamic_cast<CacheUnit*>(resPool->getResource(dataPortIdx));
1781
1782 return cache_res->read(inst, addr, data, size, flags);
1783 }
1784
1785 Fault
1786 InOrderCPU::write(DynInstPtr inst, uint8_t *data, unsigned size,
1787 Addr addr, unsigned flags, uint64_t *write_res)
1788 {
1789 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1790 // you want to run w/out caches?
1791 CacheUnit *cache_res =
1792 dynamic_cast<CacheUnit*>(resPool->getResource(dataPortIdx));
1793 return cache_res->write(inst, data, size, addr, flags, write_res);
1794 }