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