226b2d8ea0aaa91a75233f0b89f68b66565135e5
[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/quiesce_event.hh"
50 #include "cpu/simple_thread.hh"
51 #include "cpu/thread_context.hh"
52 #include "debug/Activity.hh"
53 #include "debug/InOrderCPU.hh"
54 #include "debug/RefCount.hh"
55 #include "debug/SkedCache.hh"
56 #include "debug/Quiesce.hh"
57 #include "mem/translating_port.hh"
58 #include "params/InOrderCPU.hh"
59 #include "sim/process.hh"
60 #include "sim/stat_control.hh"
61 #include "sim/system.hh"
62
63 #if THE_ISA == ALPHA_ISA
64 #include "arch/alpha/osfpal.hh"
65 #endif
66
67 using namespace std;
68 using namespace TheISA;
69 using namespace ThePipeline;
70
71 InOrderCPU::TickEvent::TickEvent(InOrderCPU *c)
72 : Event(CPU_Tick_Pri), cpu(c)
73 { }
74
75
76 void
77 InOrderCPU::TickEvent::process()
78 {
79 cpu->tick();
80 }
81
82
83 const char *
84 InOrderCPU::TickEvent::description()
85 {
86 return "InOrderCPU tick event";
87 }
88
89 InOrderCPU::CPUEvent::CPUEvent(InOrderCPU *_cpu, CPUEventType e_type,
90 Fault fault, ThreadID _tid, DynInstPtr inst,
91 CPUEventPri event_pri)
92 : Event(event_pri), cpu(_cpu)
93 {
94 setEvent(e_type, fault, _tid, inst);
95 }
96
97
98 std::string InOrderCPU::eventNames[NumCPUEvents] =
99 {
100 "ActivateThread",
101 "ActivateNextReadyThread",
102 "DeactivateThread",
103 "HaltThread",
104 "SuspendThread",
105 "Trap",
106 "Syscall",
107 "SquashFromMemStall",
108 "UpdatePCs"
109 };
110
111 void
112 InOrderCPU::CPUEvent::process()
113 {
114 switch (cpuEventType)
115 {
116 case ActivateThread:
117 cpu->activateThread(tid);
118 cpu->resPool->activateThread(tid);
119 break;
120
121 case ActivateNextReadyThread:
122 cpu->activateNextReadyThread();
123 break;
124
125 case DeactivateThread:
126 cpu->deactivateThread(tid);
127 cpu->resPool->deactivateThread(tid);
128 break;
129
130 case HaltThread:
131 cpu->haltThread(tid);
132 cpu->resPool->deactivateThread(tid);
133 break;
134
135 case SuspendThread:
136 cpu->suspendThread(tid);
137 cpu->resPool->suspendThread(tid);
138 break;
139
140 case SquashFromMemStall:
141 cpu->squashDueToMemStall(inst->squashingStage, inst->seqNum, tid);
142 cpu->resPool->squashDueToMemStall(inst, inst->squashingStage,
143 inst->seqNum, tid);
144 break;
145
146 case Trap:
147 DPRINTF(InOrderCPU, "Trapping CPU\n");
148 cpu->trap(fault, tid, inst);
149 cpu->resPool->trap(fault, tid, inst);
150 cpu->trapPending[tid] = false;
151 break;
152
153 #if !FULL_SYSTEM
154 case Syscall:
155 cpu->syscall(inst->syscallNum, tid);
156 cpu->resPool->trap(fault, tid, inst);
157 break;
158 #endif
159 default:
160 fatal("Unrecognized Event Type %s", eventNames[cpuEventType]);
161 }
162
163 cpu->cpuEventRemoveList.push(this);
164 }
165
166
167
168 const char *
169 InOrderCPU::CPUEvent::description()
170 {
171 return "InOrderCPU event";
172 }
173
174 void
175 InOrderCPU::CPUEvent::scheduleEvent(int delay)
176 {
177 assert(!scheduled() || squashed());
178 cpu->reschedule(this, cpu->nextCycle(curTick() + cpu->ticks(delay)), true);
179 }
180
181 void
182 InOrderCPU::CPUEvent::unscheduleEvent()
183 {
184 if (scheduled())
185 squash();
186 }
187
188 InOrderCPU::InOrderCPU(Params *params)
189 : BaseCPU(params),
190 cpu_id(params->cpu_id),
191 coreType("default"),
192 _status(Idle),
193 tickEvent(this),
194 stageWidth(params->stageWidth),
195 timeBuffer(2 , 2),
196 removeInstsThisCycle(false),
197 activityRec(params->name, NumStages, 10, params->activity),
198 #if FULL_SYSTEM
199 system(params->system),
200 #endif // FULL_SYSTEM
201 #ifdef DEBUG
202 cpuEventNum(0),
203 resReqCount(0),
204 #endif // DEBUG
205 drainCount(0),
206 deferRegistration(false/*params->deferRegistration*/),
207 stageTracing(params->stageTracing),
208 lastRunningCycle(0),
209 instsPerSwitch(0)
210 {
211 ThreadID active_threads;
212 cpu_params = params;
213
214 resPool = new ResourcePool(this, params);
215
216 // Resize for Multithreading CPUs
217 thread.resize(numThreads);
218
219 #if FULL_SYSTEM
220 active_threads = 1;
221 #else
222 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, NULL);
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 Fault
787 InOrderCPU::hwrei(ThreadID tid)
788 {
789 #if THE_ISA == ALPHA_ISA
790 // Need to clear the lock flag upon returning from an interrupt.
791 setMiscRegNoEffect(AlphaISA::MISCREG_LOCKFLAG, false, tid);
792
793 thread[tid]->kernelStats->hwrei();
794 // FIXME: XXX check for interrupts? XXX
795 #endif
796
797 return NoFault;
798 }
799
800
801 bool
802 InOrderCPU::simPalCheck(int palFunc, ThreadID tid)
803 {
804 #if THE_ISA == ALPHA_ISA
805 if (this->thread[tid]->kernelStats)
806 this->thread[tid]->kernelStats->callpal(palFunc,
807 this->threadContexts[tid]);
808
809 switch (palFunc) {
810 case PAL::halt:
811 halt();
812 if (--System::numSystemsRunning == 0)
813 exitSimLoop("all cpus halted");
814 break;
815
816 case PAL::bpt:
817 case PAL::bugchk:
818 if (this->system->breakpoint())
819 return false;
820 break;
821 }
822 #endif
823 return true;
824 }
825
826 void
827 InOrderCPU::checkForInterrupts()
828 {
829 for (int i = 0; i < threadContexts.size(); i++) {
830 ThreadContext *tc = threadContexts[i];
831
832 if (interrupts->checkInterrupts(tc)) {
833 Fault interrupt = interrupts->getInterrupt(tc);
834
835 if (interrupt != NoFault) {
836 DPRINTF(Interrupt, "Processing Intterupt for [tid:%i].\n",
837 tc->threadId());
838
839 ThreadID tid = tc->threadId();
840 interrupts->updateIntrInfo(tc);
841
842 // Squash from Last Stage in Pipeline
843 unsigned last_stage = NumStages - 1;
844 dummyTrapInst[tid]->squashingStage = last_stage;
845 pipelineStage[last_stage]->setupSquash(dummyTrapInst[tid],
846 tid);
847
848 // By default, setupSquash will always squash from stage + 1
849 pipelineStage[BackEndStartStage - 1]->setupSquash(dummyTrapInst[tid],
850 tid);
851
852 // Schedule Squash Through-out Resource Pool
853 resPool->scheduleEvent(
854 (InOrderCPU::CPUEventType)ResourcePool::SquashAll,
855 dummyTrapInst[tid], 0);
856
857 // Finally, Setup Trap to happen at end of cycle
858 trapContext(interrupt, tid, dummyTrapInst[tid]);
859 }
860 }
861 }
862 }
863
864 Fault
865 InOrderCPU::getInterrupts()
866 {
867 // Check if there are any outstanding interrupts
868 return interrupts->getInterrupt(threadContexts[0]);
869 }
870
871
872 void
873 InOrderCPU::processInterrupts(Fault interrupt)
874 {
875 // Check for interrupts here. For now can copy the code that
876 // exists within isa_fullsys_traits.hh. Also assume that thread 0
877 // is the one that handles the interrupts.
878 // @todo: Possibly consolidate the interrupt checking code.
879 // @todo: Allow other threads to handle interrupts.
880
881 assert(interrupt != NoFault);
882 interrupts->updateIntrInfo(threadContexts[0]);
883
884 DPRINTF(InOrderCPU, "Interrupt %s being handled\n", interrupt->name());
885
886 // Note: Context ID ok here? Impl. of FS mode needs to revisit this
887 trap(interrupt, threadContexts[0]->contextId(), dummyBufferInst);
888 }
889
890 void
891 InOrderCPU::updateMemPorts()
892 {
893 // Update all ThreadContext's memory ports (Functional/Virtual
894 // Ports)
895 ThreadID size = thread.size();
896 for (ThreadID i = 0; i < size; ++i)
897 thread[i]->connectMemPorts(thread[i]->getTC());
898 }
899
900 void
901 InOrderCPU::trapContext(Fault fault, ThreadID tid, DynInstPtr inst, int delay)
902 {
903 scheduleCpuEvent(Trap, fault, tid, inst, delay);
904 trapPending[tid] = true;
905 }
906
907 void
908 InOrderCPU::trap(Fault fault, ThreadID tid, DynInstPtr inst)
909 {
910 fault->invoke(tcBase(tid), inst->staticInst);
911 removePipelineStalls(tid);
912 }
913
914 void
915 InOrderCPU::squashFromMemStall(DynInstPtr inst, ThreadID tid, int delay)
916 {
917 scheduleCpuEvent(SquashFromMemStall, NoFault, tid, inst, delay);
918 }
919
920
921 void
922 InOrderCPU::squashDueToMemStall(int stage_num, InstSeqNum seq_num,
923 ThreadID tid)
924 {
925 DPRINTF(InOrderCPU, "Squashing Pipeline Stages Due to Memory Stall...\n");
926
927 // Squash all instructions in each stage including
928 // instruction that caused the squash (seq_num - 1)
929 // NOTE: The stage bandwidth needs to be cleared so thats why
930 // the stalling instruction is squashed as well. The stalled
931 // instruction is previously placed in another intermediate buffer
932 // while it's stall is being handled.
933 InstSeqNum squash_seq_num = seq_num - 1;
934
935 for (int stNum=stage_num; stNum >= 0 ; stNum--) {
936 pipelineStage[stNum]->squashDueToMemStall(squash_seq_num, tid);
937 }
938 }
939
940 void
941 InOrderCPU::scheduleCpuEvent(CPUEventType c_event, Fault fault,
942 ThreadID tid, DynInstPtr inst,
943 unsigned delay, CPUEventPri event_pri)
944 {
945 CPUEvent *cpu_event = new CPUEvent(this, c_event, fault, tid, inst,
946 event_pri);
947
948 Tick sked_tick = nextCycle(curTick() + ticks(delay));
949 if (delay >= 0) {
950 DPRINTF(InOrderCPU, "Scheduling CPU Event (%s) for cycle %i, [tid:%i].\n",
951 eventNames[c_event], curTick() + delay, tid);
952 schedule(cpu_event, sked_tick);
953 } else {
954 cpu_event->process();
955 cpuEventRemoveList.push(cpu_event);
956 }
957
958 // Broadcast event to the Resource Pool
959 // Need to reset tid just in case this is a dummy instruction
960 inst->setTid(tid);
961 resPool->scheduleEvent(c_event, inst, 0, 0, tid);
962 }
963
964 bool
965 InOrderCPU::isThreadActive(ThreadID tid)
966 {
967 list<ThreadID>::iterator isActive =
968 std::find(activeThreads.begin(), activeThreads.end(), tid);
969
970 return (isActive != activeThreads.end());
971 }
972
973 bool
974 InOrderCPU::isThreadReady(ThreadID tid)
975 {
976 list<ThreadID>::iterator isReady =
977 std::find(readyThreads.begin(), readyThreads.end(), tid);
978
979 return (isReady != readyThreads.end());
980 }
981
982 bool
983 InOrderCPU::isThreadSuspended(ThreadID tid)
984 {
985 list<ThreadID>::iterator isSuspended =
986 std::find(suspendedThreads.begin(), suspendedThreads.end(), tid);
987
988 return (isSuspended != suspendedThreads.end());
989 }
990
991 void
992 InOrderCPU::activateNextReadyThread()
993 {
994 if (readyThreads.size() >= 1) {
995 ThreadID ready_tid = readyThreads.front();
996
997 // Activate in Pipeline
998 activateThread(ready_tid);
999
1000 // Activate in Resource Pool
1001 resPool->activateThread(ready_tid);
1002
1003 list<ThreadID>::iterator ready_it =
1004 std::find(readyThreads.begin(), readyThreads.end(), ready_tid);
1005 readyThreads.erase(ready_it);
1006 } else {
1007 DPRINTF(InOrderCPU,
1008 "Attempting to activate new thread, but No Ready Threads to"
1009 "activate.\n");
1010 DPRINTF(InOrderCPU,
1011 "Unable to switch to next active thread.\n");
1012 }
1013 }
1014
1015 void
1016 InOrderCPU::activateThread(ThreadID tid)
1017 {
1018 if (isThreadSuspended(tid)) {
1019 DPRINTF(InOrderCPU,
1020 "Removing [tid:%i] from suspended threads list.\n", tid);
1021
1022 list<ThreadID>::iterator susp_it =
1023 std::find(suspendedThreads.begin(), suspendedThreads.end(),
1024 tid);
1025 suspendedThreads.erase(susp_it);
1026 }
1027
1028 if (threadModel == SwitchOnCacheMiss &&
1029 numActiveThreads() == 1) {
1030 DPRINTF(InOrderCPU,
1031 "Ignoring activation of [tid:%i], since [tid:%i] is "
1032 "already running.\n", tid, activeThreadId());
1033
1034 DPRINTF(InOrderCPU,"Placing [tid:%i] on ready threads list\n",
1035 tid);
1036
1037 readyThreads.push_back(tid);
1038
1039 } else if (!isThreadActive(tid)) {
1040 DPRINTF(InOrderCPU,
1041 "Adding [tid:%i] to active threads list.\n", tid);
1042 activeThreads.push_back(tid);
1043
1044 activateThreadInPipeline(tid);
1045
1046 thread[tid]->lastActivate = curTick();
1047
1048 tcBase(tid)->setStatus(ThreadContext::Active);
1049
1050 wakeCPU();
1051
1052 numCtxtSwitches++;
1053 }
1054 }
1055
1056 void
1057 InOrderCPU::activateThreadInPipeline(ThreadID tid)
1058 {
1059 for (int stNum=0; stNum < NumStages; stNum++) {
1060 pipelineStage[stNum]->activateThread(tid);
1061 }
1062 }
1063
1064 void
1065 InOrderCPU::deactivateContext(ThreadID tid, int delay)
1066 {
1067 DPRINTF(InOrderCPU,"[tid:%i]: Deactivating ...\n", tid);
1068
1069 scheduleCpuEvent(DeactivateThread, NoFault, tid, dummyInst[tid], delay);
1070
1071 // Be sure to signal that there's some activity so the CPU doesn't
1072 // deschedule itself.
1073 activityRec.activity();
1074
1075 _status = Running;
1076 }
1077
1078 void
1079 InOrderCPU::deactivateThread(ThreadID tid)
1080 {
1081 DPRINTF(InOrderCPU, "[tid:%i]: Calling deactivate thread.\n", tid);
1082
1083 if (isThreadActive(tid)) {
1084 DPRINTF(InOrderCPU,"[tid:%i]: Removing from active threads list\n",
1085 tid);
1086 list<ThreadID>::iterator thread_it =
1087 std::find(activeThreads.begin(), activeThreads.end(), tid);
1088
1089 removePipelineStalls(*thread_it);
1090
1091 activeThreads.erase(thread_it);
1092
1093 // Ideally, this should be triggered from the
1094 // suspendContext/Thread functions
1095 tcBase(tid)->setStatus(ThreadContext::Suspended);
1096 }
1097
1098 assert(!isThreadActive(tid));
1099 }
1100
1101 void
1102 InOrderCPU::removePipelineStalls(ThreadID tid)
1103 {
1104 DPRINTF(InOrderCPU,"[tid:%i]: Removing all pipeline stalls\n",
1105 tid);
1106
1107 for (int stNum = 0; stNum < NumStages ; stNum++) {
1108 pipelineStage[stNum]->removeStalls(tid);
1109 }
1110
1111 }
1112
1113 void
1114 InOrderCPU::updateThreadPriority()
1115 {
1116 if (activeThreads.size() > 1)
1117 {
1118 //DEFAULT TO ROUND ROBIN SCHEME
1119 //e.g. Move highest priority to end of thread list
1120 list<ThreadID>::iterator list_begin = activeThreads.begin();
1121 list<ThreadID>::iterator list_end = activeThreads.end();
1122
1123 unsigned high_thread = *list_begin;
1124
1125 activeThreads.erase(list_begin);
1126
1127 activeThreads.push_back(high_thread);
1128 }
1129 }
1130
1131 inline void
1132 InOrderCPU::tickThreadStats()
1133 {
1134 /** Keep track of cycles that each thread is active */
1135 list<ThreadID>::iterator thread_it = activeThreads.begin();
1136 while (thread_it != activeThreads.end()) {
1137 threadCycles[*thread_it]++;
1138 thread_it++;
1139 }
1140
1141 // Keep track of cycles where SMT is active
1142 if (activeThreads.size() > 1) {
1143 smtCycles++;
1144 }
1145 }
1146
1147 void
1148 InOrderCPU::activateContext(ThreadID tid, int delay)
1149 {
1150 DPRINTF(InOrderCPU,"[tid:%i]: Activating ...\n", tid);
1151
1152
1153 scheduleCpuEvent(ActivateThread, NoFault, tid, dummyInst[tid], delay);
1154
1155 // Be sure to signal that there's some activity so the CPU doesn't
1156 // deschedule itself.
1157 activityRec.activity();
1158
1159 _status = Running;
1160 }
1161
1162 void
1163 InOrderCPU::activateNextReadyContext(int delay)
1164 {
1165 DPRINTF(InOrderCPU,"Activating next ready thread\n");
1166
1167 scheduleCpuEvent(ActivateNextReadyThread, NoFault, 0/*tid*/, dummyInst[0],
1168 delay, ActivateNextReadyThread_Pri);
1169
1170 // Be sure to signal that there's some activity so the CPU doesn't
1171 // deschedule itself.
1172 activityRec.activity();
1173
1174 _status = Running;
1175 }
1176
1177 void
1178 InOrderCPU::haltContext(ThreadID tid, int delay)
1179 {
1180 DPRINTF(InOrderCPU, "[tid:%i]: Calling Halt Context...\n", tid);
1181
1182 scheduleCpuEvent(HaltThread, NoFault, tid, dummyInst[tid], delay);
1183
1184 activityRec.activity();
1185 }
1186
1187 void
1188 InOrderCPU::haltThread(ThreadID tid)
1189 {
1190 DPRINTF(InOrderCPU, "[tid:%i]: Placing on Halted Threads List...\n", tid);
1191 deactivateThread(tid);
1192 squashThreadInPipeline(tid);
1193 haltedThreads.push_back(tid);
1194
1195 tcBase(tid)->setStatus(ThreadContext::Halted);
1196
1197 if (threadModel == SwitchOnCacheMiss) {
1198 activateNextReadyContext();
1199 }
1200 }
1201
1202 void
1203 InOrderCPU::suspendContext(ThreadID tid, int delay)
1204 {
1205 scheduleCpuEvent(SuspendThread, NoFault, tid, dummyInst[tid], delay);
1206 }
1207
1208 void
1209 InOrderCPU::suspendThread(ThreadID tid)
1210 {
1211 DPRINTF(InOrderCPU, "[tid:%i]: Placing on Suspended Threads List...\n",
1212 tid);
1213 deactivateThread(tid);
1214 suspendedThreads.push_back(tid);
1215 thread[tid]->lastSuspend = curTick();
1216
1217 tcBase(tid)->setStatus(ThreadContext::Suspended);
1218 }
1219
1220 void
1221 InOrderCPU::squashThreadInPipeline(ThreadID tid)
1222 {
1223 //Squash all instructions in each stage
1224 for (int stNum=NumStages - 1; stNum >= 0 ; stNum--) {
1225 pipelineStage[stNum]->squash(0 /*seq_num*/, tid);
1226 }
1227 }
1228
1229 PipelineStage*
1230 InOrderCPU::getPipeStage(int stage_num)
1231 {
1232 return pipelineStage[stage_num];
1233 }
1234
1235
1236 RegIndex
1237 InOrderCPU::flattenRegIdx(RegIndex reg_idx, RegType &reg_type, ThreadID tid)
1238 {
1239 if (reg_idx < FP_Base_DepTag) {
1240 reg_type = IntType;
1241 return isa[tid].flattenIntIndex(reg_idx);
1242 } else if (reg_idx < Ctrl_Base_DepTag) {
1243 reg_type = FloatType;
1244 reg_idx -= FP_Base_DepTag;
1245 return isa[tid].flattenFloatIndex(reg_idx);
1246 } else {
1247 reg_type = MiscType;
1248 return reg_idx - TheISA::Ctrl_Base_DepTag;
1249 }
1250 }
1251
1252 uint64_t
1253 InOrderCPU::readIntReg(RegIndex reg_idx, ThreadID tid)
1254 {
1255 DPRINTF(IntRegs, "[tid:%i]: Reading Int. Reg %i as %x\n",
1256 tid, reg_idx, intRegs[tid][reg_idx]);
1257
1258 return intRegs[tid][reg_idx];
1259 }
1260
1261 FloatReg
1262 InOrderCPU::readFloatReg(RegIndex reg_idx, ThreadID tid)
1263 {
1264 DPRINTF(FloatRegs, "[tid:%i]: Reading Float Reg %i as %x, %08f\n",
1265 tid, reg_idx, floatRegs.i[tid][reg_idx], floatRegs.f[tid][reg_idx]);
1266
1267 return floatRegs.f[tid][reg_idx];
1268 }
1269
1270 FloatRegBits
1271 InOrderCPU::readFloatRegBits(RegIndex reg_idx, ThreadID tid)
1272 {
1273 DPRINTF(FloatRegs, "[tid:%i]: Reading Float Reg %i as %x, %08f\n",
1274 tid, reg_idx, floatRegs.i[tid][reg_idx], floatRegs.f[tid][reg_idx]);
1275
1276 return floatRegs.i[tid][reg_idx];
1277 }
1278
1279 void
1280 InOrderCPU::setIntReg(RegIndex reg_idx, uint64_t val, ThreadID tid)
1281 {
1282 if (reg_idx == TheISA::ZeroReg) {
1283 DPRINTF(IntRegs, "[tid:%i]: Ignoring Setting of ISA-ZeroReg "
1284 "(Int. Reg %i) to %x\n", tid, reg_idx, val);
1285 return;
1286 } else {
1287 DPRINTF(IntRegs, "[tid:%i]: Setting Int. Reg %i to %x\n",
1288 tid, reg_idx, val);
1289
1290 intRegs[tid][reg_idx] = val;
1291 }
1292 }
1293
1294
1295 void
1296 InOrderCPU::setFloatReg(RegIndex reg_idx, FloatReg val, ThreadID tid)
1297 {
1298 floatRegs.f[tid][reg_idx] = val;
1299 DPRINTF(FloatRegs, "[tid:%i]: Setting Float. Reg %i bits to "
1300 "%x, %08f\n",
1301 tid, reg_idx,
1302 floatRegs.i[tid][reg_idx],
1303 floatRegs.f[tid][reg_idx]);
1304 }
1305
1306
1307 void
1308 InOrderCPU::setFloatRegBits(RegIndex reg_idx, FloatRegBits val, ThreadID tid)
1309 {
1310 floatRegs.i[tid][reg_idx] = val;
1311 DPRINTF(FloatRegs, "[tid:%i]: Setting Float. Reg %i bits to "
1312 "%x, %08f\n",
1313 tid, reg_idx,
1314 floatRegs.i[tid][reg_idx],
1315 floatRegs.f[tid][reg_idx]);
1316 }
1317
1318 uint64_t
1319 InOrderCPU::readRegOtherThread(unsigned reg_idx, ThreadID tid)
1320 {
1321 // If Default value is set, then retrieve target thread
1322 if (tid == InvalidThreadID) {
1323 tid = TheISA::getTargetThread(tcBase(tid));
1324 }
1325
1326 if (reg_idx < FP_Base_DepTag) {
1327 // Integer Register File
1328 return readIntReg(reg_idx, tid);
1329 } else if (reg_idx < Ctrl_Base_DepTag) {
1330 // Float Register File
1331 reg_idx -= FP_Base_DepTag;
1332 return readFloatRegBits(reg_idx, tid);
1333 } else {
1334 reg_idx -= Ctrl_Base_DepTag;
1335 return readMiscReg(reg_idx, tid); // Misc. Register File
1336 }
1337 }
1338 void
1339 InOrderCPU::setRegOtherThread(unsigned reg_idx, const MiscReg &val,
1340 ThreadID tid)
1341 {
1342 // If Default value is set, then retrieve target thread
1343 if (tid == InvalidThreadID) {
1344 tid = TheISA::getTargetThread(tcBase(tid));
1345 }
1346
1347 if (reg_idx < FP_Base_DepTag) { // Integer Register File
1348 setIntReg(reg_idx, val, tid);
1349 } else if (reg_idx < Ctrl_Base_DepTag) { // Float Register File
1350 reg_idx -= FP_Base_DepTag;
1351 setFloatRegBits(reg_idx, val, tid);
1352 } else {
1353 reg_idx -= Ctrl_Base_DepTag;
1354 setMiscReg(reg_idx, val, tid); // Misc. Register File
1355 }
1356 }
1357
1358 MiscReg
1359 InOrderCPU::readMiscRegNoEffect(int misc_reg, ThreadID tid)
1360 {
1361 return isa[tid].readMiscRegNoEffect(misc_reg);
1362 }
1363
1364 MiscReg
1365 InOrderCPU::readMiscReg(int misc_reg, ThreadID tid)
1366 {
1367 return isa[tid].readMiscReg(misc_reg, tcBase(tid));
1368 }
1369
1370 void
1371 InOrderCPU::setMiscRegNoEffect(int misc_reg, const MiscReg &val, ThreadID tid)
1372 {
1373 isa[tid].setMiscRegNoEffect(misc_reg, val);
1374 }
1375
1376 void
1377 InOrderCPU::setMiscReg(int misc_reg, const MiscReg &val, ThreadID tid)
1378 {
1379 isa[tid].setMiscReg(misc_reg, val, tcBase(tid));
1380 }
1381
1382
1383 InOrderCPU::ListIt
1384 InOrderCPU::addInst(DynInstPtr inst)
1385 {
1386 ThreadID tid = inst->readTid();
1387
1388 instList[tid].push_back(inst);
1389
1390 return --(instList[tid].end());
1391 }
1392
1393 InOrderCPU::ListIt
1394 InOrderCPU::findInst(InstSeqNum seq_num, ThreadID tid)
1395 {
1396 ListIt it = instList[tid].begin();
1397 ListIt end = instList[tid].end();
1398
1399 while (it != end) {
1400 if ((*it)->seqNum == seq_num)
1401 return it;
1402 else if ((*it)->seqNum > seq_num)
1403 break;
1404
1405 it++;
1406 }
1407
1408 return instList[tid].end();
1409 }
1410
1411 void
1412 InOrderCPU::updateContextSwitchStats()
1413 {
1414 // Set Average Stat Here, then reset to 0
1415 instsPerCtxtSwitch = instsPerSwitch;
1416 instsPerSwitch = 0;
1417 }
1418
1419
1420 void
1421 InOrderCPU::instDone(DynInstPtr inst, ThreadID tid)
1422 {
1423 // Set the nextPC to be fetched if this is the last instruction
1424 // committed
1425 // ========
1426 // This contributes to the precise state of the CPU
1427 // which can be used when restoring a thread to the CPU after after any
1428 // type of context switching activity (fork, exception, etc.)
1429 TheISA::PCState comm_pc = inst->pcState();
1430 lastCommittedPC[tid] = comm_pc;
1431 TheISA::advancePC(comm_pc, inst->staticInst);
1432 pcState(comm_pc, tid);
1433
1434 //@todo: may be unnecessary with new-ISA-specific branch handling code
1435 if (inst->isControl()) {
1436 thread[tid]->lastGradIsBranch = true;
1437 thread[tid]->lastBranchPC = inst->pcState();
1438 TheISA::advancePC(thread[tid]->lastBranchPC, inst->staticInst);
1439 } else {
1440 thread[tid]->lastGradIsBranch = false;
1441 }
1442
1443
1444 // Finalize Trace Data For Instruction
1445 if (inst->traceData) {
1446 //inst->traceData->setCycle(curTick());
1447 inst->traceData->setFetchSeq(inst->seqNum);
1448 //inst->traceData->setCPSeq(cpu->tcBase(tid)->numInst);
1449 inst->traceData->dump();
1450 delete inst->traceData;
1451 inst->traceData = NULL;
1452 }
1453
1454 // Increment active thread's instruction count
1455 instsPerSwitch++;
1456
1457 // Increment thread-state's instruction count
1458 thread[tid]->numInst++;
1459
1460 // Increment thread-state's instruction stats
1461 thread[tid]->numInsts++;
1462
1463 // Count committed insts per thread stats
1464 committedInsts[tid]++;
1465
1466 // Count total insts committed stat
1467 totalCommittedInsts++;
1468
1469 // Count SMT-committed insts per thread stat
1470 if (numActiveThreads() > 1) {
1471 smtCommittedInsts[tid]++;
1472 }
1473
1474 // Instruction-Mix Stats
1475 if (inst->isLoad()) {
1476 comLoads++;
1477 } else if (inst->isStore()) {
1478 comStores++;
1479 } else if (inst->isControl()) {
1480 comBranches++;
1481 } else if (inst->isNop()) {
1482 comNops++;
1483 } else if (inst->isNonSpeculative()) {
1484 comNonSpec++;
1485 } else if (inst->isInteger()) {
1486 comInts++;
1487 } else if (inst->isFloating()) {
1488 comFloats++;
1489 }
1490
1491 // Check for instruction-count-based events.
1492 comInstEventQueue[tid]->serviceEvents(thread[tid]->numInst);
1493
1494 // Finally, remove instruction from CPU
1495 removeInst(inst);
1496 }
1497
1498 // currently unused function, but substitute repetitive code w/this function
1499 // call
1500 void
1501 InOrderCPU::addToRemoveList(DynInstPtr inst)
1502 {
1503 removeInstsThisCycle = true;
1504 if (!inst->isRemoveList()) {
1505 DPRINTF(InOrderCPU, "Pushing instruction [tid:%i] PC %s "
1506 "[sn:%lli] to remove list\n",
1507 inst->threadNumber, inst->pcState(), inst->seqNum);
1508 inst->setRemoveList();
1509 removeList.push(inst->getInstListIt());
1510 } else {
1511 DPRINTF(InOrderCPU, "Ignoring instruction removal for [tid:%i] PC %s "
1512 "[sn:%lli], already remove list\n",
1513 inst->threadNumber, inst->pcState(), inst->seqNum);
1514 }
1515
1516 }
1517
1518 void
1519 InOrderCPU::removeInst(DynInstPtr inst)
1520 {
1521 DPRINTF(InOrderCPU, "Removing graduated instruction [tid:%i] PC %s "
1522 "[sn:%lli]\n",
1523 inst->threadNumber, inst->pcState(), inst->seqNum);
1524
1525 removeInstsThisCycle = true;
1526
1527 // Remove the instruction.
1528 if (!inst->isRemoveList()) {
1529 DPRINTF(InOrderCPU, "Pushing instruction [tid:%i] PC %s "
1530 "[sn:%lli] to remove list\n",
1531 inst->threadNumber, inst->pcState(), inst->seqNum);
1532 inst->setRemoveList();
1533 removeList.push(inst->getInstListIt());
1534 } else {
1535 DPRINTF(InOrderCPU, "Ignoring instruction removal for [tid:%i] PC %s "
1536 "[sn:%lli], already on remove list\n",
1537 inst->threadNumber, inst->pcState(), inst->seqNum);
1538 }
1539
1540 }
1541
1542 void
1543 InOrderCPU::removeInstsUntil(const InstSeqNum &seq_num, ThreadID tid)
1544 {
1545 //assert(!instList[tid].empty());
1546
1547 removeInstsThisCycle = true;
1548
1549 ListIt inst_iter = instList[tid].end();
1550
1551 inst_iter--;
1552
1553 DPRINTF(InOrderCPU, "Squashing instructions from CPU instruction "
1554 "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
1555 tid, seq_num, (*inst_iter)->seqNum);
1556
1557 while ((*inst_iter)->seqNum > seq_num) {
1558
1559 bool break_loop = (inst_iter == instList[tid].begin());
1560
1561 squashInstIt(inst_iter, tid);
1562
1563 inst_iter--;
1564
1565 if (break_loop)
1566 break;
1567 }
1568 }
1569
1570
1571 inline void
1572 InOrderCPU::squashInstIt(const ListIt inst_it, ThreadID tid)
1573 {
1574 DynInstPtr inst = (*inst_it);
1575 if (inst->threadNumber == tid) {
1576 DPRINTF(InOrderCPU, "Squashing instruction, "
1577 "[tid:%i] [sn:%lli] PC %s\n",
1578 inst->threadNumber,
1579 inst->seqNum,
1580 inst->pcState());
1581
1582 inst->setSquashed();
1583 archRegDepMap[tid].remove(inst);
1584
1585 if (!inst->isRemoveList()) {
1586 DPRINTF(InOrderCPU, "Pushing instruction [tid:%i] PC %s "
1587 "[sn:%lli] to remove list\n",
1588 inst->threadNumber, inst->pcState(),
1589 inst->seqNum);
1590 inst->setRemoveList();
1591 removeList.push(inst_it);
1592 } else {
1593 DPRINTF(InOrderCPU, "Ignoring instruction removal for [tid:%i]"
1594 " PC %s [sn:%lli], already on remove list\n",
1595 inst->threadNumber, inst->pcState(),
1596 inst->seqNum);
1597 }
1598
1599 }
1600
1601 }
1602
1603
1604 void
1605 InOrderCPU::cleanUpRemovedInsts()
1606 {
1607 while (!removeList.empty()) {
1608 DPRINTF(InOrderCPU, "Removing instruction, "
1609 "[tid:%i] [sn:%lli] PC %s\n",
1610 (*removeList.front())->threadNumber,
1611 (*removeList.front())->seqNum,
1612 (*removeList.front())->pcState());
1613
1614 DynInstPtr inst = *removeList.front();
1615 ThreadID tid = inst->threadNumber;
1616
1617 // Remove From Register Dependency Map, If Necessary
1618 // archRegDepMap[tid].remove(inst);
1619
1620 // Clear if Non-Speculative
1621 if (inst->staticInst &&
1622 inst->seqNum == nonSpecSeqNum[tid] &&
1623 nonSpecInstActive[tid] == true) {
1624 nonSpecInstActive[tid] = false;
1625 }
1626
1627 inst->onInstList = false;
1628
1629 instList[tid].erase(removeList.front());
1630
1631 removeList.pop();
1632 }
1633
1634 removeInstsThisCycle = false;
1635 }
1636
1637 void
1638 InOrderCPU::cleanUpRemovedEvents()
1639 {
1640 while (!cpuEventRemoveList.empty()) {
1641 Event *cpu_event = cpuEventRemoveList.front();
1642 cpuEventRemoveList.pop();
1643 delete cpu_event;
1644 }
1645 }
1646
1647
1648 void
1649 InOrderCPU::dumpInsts()
1650 {
1651 int num = 0;
1652
1653 ListIt inst_list_it = instList[0].begin();
1654
1655 cprintf("Dumping Instruction List\n");
1656
1657 while (inst_list_it != instList[0].end()) {
1658 cprintf("Instruction:%i\nPC:%s\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
1659 "Squashed:%i\n\n",
1660 num, (*inst_list_it)->pcState(),
1661 (*inst_list_it)->threadNumber,
1662 (*inst_list_it)->seqNum, (*inst_list_it)->isIssued(),
1663 (*inst_list_it)->isSquashed());
1664 inst_list_it++;
1665 ++num;
1666 }
1667 }
1668
1669 void
1670 InOrderCPU::wakeCPU()
1671 {
1672 if (/*activityRec.active() || */tickEvent.scheduled()) {
1673 DPRINTF(Activity, "CPU already running.\n");
1674 return;
1675 }
1676
1677 DPRINTF(Activity, "Waking up CPU\n");
1678
1679 Tick extra_cycles = tickToCycles((curTick() - 1) - lastRunningCycle);
1680
1681 idleCycles += extra_cycles;
1682 for (int stage_num = 0; stage_num < NumStages; stage_num++) {
1683 pipelineStage[stage_num]->idleCycles += extra_cycles;
1684 }
1685
1686 numCycles += extra_cycles;
1687
1688 schedule(&tickEvent, nextCycle(curTick()));
1689 }
1690
1691 // Lots of copied full system code...place into BaseCPU class?
1692 void
1693 InOrderCPU::wakeup()
1694 {
1695 if (thread[0]->status() != ThreadContext::Suspended)
1696 return;
1697
1698 wakeCPU();
1699
1700 DPRINTF(Quiesce, "Suspended Processor woken\n");
1701 threadContexts[0]->activate();
1702 }
1703
1704 void
1705 InOrderCPU::syscallContext(Fault fault, ThreadID tid, DynInstPtr inst, int delay)
1706 {
1707 // Syscall must be non-speculative, so squash from last stage
1708 unsigned squash_stage = NumStages - 1;
1709 inst->setSquashInfo(squash_stage);
1710
1711 // Squash In Pipeline Stage
1712 pipelineStage[squash_stage]->setupSquash(inst, tid);
1713
1714 // Schedule Squash Through-out Resource Pool
1715 resPool->scheduleEvent(
1716 (InOrderCPU::CPUEventType)ResourcePool::SquashAll, inst, 0);
1717 scheduleCpuEvent(Syscall, fault, tid, inst, delay, Syscall_Pri);
1718 }
1719
1720 void
1721 InOrderCPU::syscall(int64_t callnum, ThreadID tid)
1722 {
1723 DPRINTF(InOrderCPU, "[tid:%i] Executing syscall().\n\n", tid);
1724
1725 DPRINTF(Activity,"Activity: syscall() called.\n");
1726
1727 // Temporarily increase this by one to account for the syscall
1728 // instruction.
1729 ++(this->thread[tid]->funcExeInst);
1730
1731 // Execute the actual syscall.
1732 this->thread[tid]->syscall(callnum);
1733
1734 // Decrease funcExeInst by one as the normal commit will handle
1735 // incrementing it.
1736 --(this->thread[tid]->funcExeInst);
1737
1738 // Clear Non-Speculative Block Variable
1739 nonSpecInstActive[tid] = false;
1740 }
1741
1742 TheISA::TLB*
1743 InOrderCPU::getITBPtr()
1744 {
1745 CacheUnit *itb_res =
1746 dynamic_cast<CacheUnit*>(resPool->getResource(fetchPortIdx));
1747 return itb_res->tlb();
1748 }
1749
1750
1751 TheISA::TLB*
1752 InOrderCPU::getDTBPtr()
1753 {
1754 CacheUnit *dtb_res =
1755 dynamic_cast<CacheUnit*>(resPool->getResource(dataPortIdx));
1756 return dtb_res->tlb();
1757 }
1758
1759 Decoder *
1760 InOrderCPU::getDecoderPtr()
1761 {
1762 FetchUnit *fetch_res =
1763 dynamic_cast<FetchUnit*>(resPool->getResource(fetchPortIdx));
1764 return &fetch_res->decoder;
1765 }
1766
1767 Fault
1768 InOrderCPU::read(DynInstPtr inst, Addr addr,
1769 uint8_t *data, unsigned size, unsigned flags)
1770 {
1771 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1772 // you want to run w/out caches?
1773 CacheUnit *cache_res =
1774 dynamic_cast<CacheUnit*>(resPool->getResource(dataPortIdx));
1775
1776 return cache_res->read(inst, addr, data, size, flags);
1777 }
1778
1779 Fault
1780 InOrderCPU::write(DynInstPtr inst, uint8_t *data, unsigned size,
1781 Addr addr, unsigned flags, uint64_t *write_res)
1782 {
1783 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1784 // you want to run w/out caches?
1785 CacheUnit *cache_res =
1786 dynamic_cast<CacheUnit*>(resPool->getResource(dataPortIdx));
1787 return cache_res->write(inst, data, size, addr, flags, write_res);
1788 }