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