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