blob: 69aea0c57775e7d6247373b55cda6ac88687de52 [file] [log] [blame]
Korey Sewell973d8b82009-02-10 15:49:29 -08001/*
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
Nathan Binkert47877cf2009-05-26 09:23:13 -070032#include <algorithm>
Steve Reinhardt14808ec2009-04-17 16:54:58 -070033
Korey Sewell973d8b82009-02-10 15:49:29 -080034#include "arch/utility.hh"
Nathan Binkert47877cf2009-05-26 09:23:13 -070035#include "config/full_system.hh"
Nathan Binkertd9f39c82009-09-23 08:34:21 -070036#include "config/the_isa.hh"
Korey Sewell973d8b82009-02-10 15:49:29 -080037#include "cpu/activity.hh"
Korey Sewell973d8b82009-02-10 15:49:29 -080038#include "cpu/base.hh"
Nathan Binkertd9f39c82009-09-23 08:34:21 -070039#include "cpu/exetrace.hh"
40#include "cpu/inorder/cpu.hh"
41#include "cpu/inorder/first_stage.hh"
Korey Sewell973d8b82009-02-10 15:49:29 -080042#include "cpu/inorder/inorder_dyn_inst.hh"
Nathan Binkertd9f39c82009-09-23 08:34:21 -070043#include "cpu/inorder/pipeline_traits.hh"
44#include "cpu/inorder/resource_pool.hh"
45#include "cpu/inorder/resources/resource_list.hh"
Korey Sewell973d8b82009-02-10 15:49:29 -080046#include "cpu/inorder/thread_context.hh"
47#include "cpu/inorder/thread_state.hh"
Nathan Binkertd9f39c82009-09-23 08:34:21 -070048#include "cpu/simple_thread.hh"
49#include "cpu/thread_context.hh"
Korey Sewell973d8b82009-02-10 15:49:29 -080050#include "mem/translating_port.hh"
Nathan Binkertd9f39c82009-09-23 08:34:21 -070051#include "params/InOrderCPU.hh"
Korey Sewell973d8b82009-02-10 15:49:29 -080052#include "sim/process.hh"
Korey Sewell973d8b82009-02-10 15:49:29 -080053#include "sim/stat_control.hh"
Korey Sewell973d8b82009-02-10 15:49:29 -080054
Korey Sewellbadb2382009-09-15 01:44:48 -040055#if FULL_SYSTEM
56#include "cpu/quiesce_event.hh"
57#include "sim/system.hh"
58#endif
59
60#if THE_ISA == ALPHA_ISA
61#include "arch/alpha/osfpal.hh"
62#endif
63
Korey Sewell973d8b82009-02-10 15:49:29 -080064using namespace std;
65using namespace TheISA;
66using namespace ThePipeline;
67
68InOrderCPU::TickEvent::TickEvent(InOrderCPU *c)
69 : Event(CPU_Tick_Pri), cpu(c)
70{ }
71
72
73void
74InOrderCPU::TickEvent::process()
75{
76 cpu->tick();
77}
78
79
80const char *
81InOrderCPU::TickEvent::description()
82{
83 return "InOrderCPU tick event";
84}
85
86InOrderCPU::CPUEvent::CPUEvent(InOrderCPU *_cpu, CPUEventType e_type,
Korey Sewelld8e09352010-01-31 18:26:03 -050087 Fault fault, ThreadID _tid, DynInstPtr inst)
Korey Sewell973d8b82009-02-10 15:49:29 -080088 : Event(CPU_Tick_Pri), cpu(_cpu)
89{
Korey Sewelld8e09352010-01-31 18:26:03 -050090 setEvent(e_type, fault, _tid, inst);
Korey Sewell973d8b82009-02-10 15:49:29 -080091}
92
Korey Sewell3a057bd2009-05-12 15:01:16 -040093
94std::string InOrderCPU::eventNames[NumCPUEvents] =
95{
96 "ActivateThread",
97 "DeallocateThread",
98 "SuspendThread",
99 "DisableThreads",
100 "EnableThreads",
101 "DisableVPEs",
102 "EnableVPEs",
103 "Trap",
104 "InstGraduated",
105 "SquashAll",
106 "UpdatePCs"
107};
108
Korey Sewell973d8b82009-02-10 15:49:29 -0800109void
110InOrderCPU::CPUEvent::process()
111{
112 switch (cpuEventType)
113 {
114 case ActivateThread:
115 cpu->activateThread(tid);
116 break;
117
Korey Sewell7b3b3622010-01-31 17:18:15 -0500118 //@TODO: Consider Implementing "Suspend Thread" as Separate from
119 //Deallocate
Korey Sewell973d8b82009-02-10 15:49:29 -0800120 case SuspendThread: // Suspend & Deallocate are same for now.
121 //cpu->suspendThread(tid);
122 //break;
123 case DeallocateThread:
124 cpu->deallocateThread(tid);
125 break;
126
127 case EnableVPEs:
128 cpu->enableVPEs(vpe);
129 break;
130
131 case DisableVPEs:
132 cpu->disableVPEs(tid, vpe);
133 break;
134
135 case EnableThreads:
136 cpu->enableThreads(vpe);
137 break;
138
139 case DisableThreads:
140 cpu->disableThreads(tid, vpe);
141 break;
142
143 case Trap:
144 cpu->trapCPU(fault, tid);
145 break;
146
147 default:
148 fatal("Unrecognized Event Type %d", cpuEventType);
Korey Sewell7b3b3622010-01-31 17:18:15 -0500149
Korey Sewell973d8b82009-02-10 15:49:29 -0800150 }
Korey Sewell7b3b3622010-01-31 17:18:15 -0500151
Korey Sewell973d8b82009-02-10 15:49:29 -0800152 cpu->cpuEventRemoveList.push(this);
153}
154
Korey Sewell7b3b3622010-01-31 17:18:15 -0500155
156
Korey Sewell973d8b82009-02-10 15:49:29 -0800157const char *
158InOrderCPU::CPUEvent::description()
159{
160 return "InOrderCPU event";
161}
162
163void
164InOrderCPU::CPUEvent::scheduleEvent(int delay)
165{
166 if (squashed())
167 mainEventQueue.reschedule(this,curTick + cpu->ticks(delay));
168 else if (!scheduled())
169 mainEventQueue.schedule(this,curTick + cpu->ticks(delay));
170}
171
172void
173InOrderCPU::CPUEvent::unscheduleEvent()
174{
175 if (scheduled())
176 squash();
177}
178
179InOrderCPU::InOrderCPU(Params *params)
180 : BaseCPU(params),
181 cpu_id(params->cpu_id),
Korey Sewell9e1dc7f2009-03-04 22:37:45 -0500182 coreType("default"),
183 _status(Idle),
Korey Sewell973d8b82009-02-10 15:49:29 -0800184 tickEvent(this),
Korey Sewell973d8b82009-02-10 15:49:29 -0800185 timeBuffer(2 , 2),
186 removeInstsThisCycle(false),
187 activityRec(params->name, NumStages, 10, params->activity),
Korey Sewellbadb2382009-09-15 01:44:48 -0400188#if FULL_SYSTEM
189 system(params->system),
190 physmem(system->physmem),
191#endif // FULL_SYSTEM
Korey Sewell7b3b3622010-01-31 17:18:15 -0500192#ifdef DEBUG
193 cpuEventNum(0),
194 resReqCount(0),
195#endif // DEBUG
Korey Sewell973d8b82009-02-10 15:49:29 -0800196 switchCount(0),
197 deferRegistration(false/*params->deferRegistration*/),
198 stageTracing(params->stageTracing),
Korey Sewell973d8b82009-02-10 15:49:29 -0800199 numVirtProcs(1)
Korey Sewell0e967982010-01-31 18:25:13 -0500200{
Korey Sewellbadb2382009-09-15 01:44:48 -0400201 ThreadID active_threads;
Korey Sewell973d8b82009-02-10 15:49:29 -0800202 cpu_params = params;
203
204 resPool = new ResourcePool(this, params);
Korey Sewell973d8b82009-02-10 15:49:29 -0800205
Korey Sewell973d8b82009-02-10 15:49:29 -0800206 // Resize for Multithreading CPUs
207 thread.resize(numThreads);
208
Korey Sewellbadb2382009-09-15 01:44:48 -0400209#if FULL_SYSTEM
210 active_threads = 1;
211#else
212 active_threads = params->workload.size();
Korey Sewell973d8b82009-02-10 15:49:29 -0800213
214 if (active_threads > MaxThreads) {
215 panic("Workload Size too large. Increase the 'MaxThreads'"
216 "in your InOrder implementation or "
217 "edit your workload size.");
218 }
Korey Sewell0e967982010-01-31 18:25:13 -0500219
220 if (active_threads > 1) {
221 threadModel = (InOrderCPU::ThreadModel) params->threadModel;
222 } else {
223 threadModel = Single;
224 }
225
226
227
Korey Sewellbadb2382009-09-15 01:44:48 -0400228#endif
Korey Sewell973d8b82009-02-10 15:49:29 -0800229
230 // Bind the fetch & data ports from the resource pool.
231 fetchPortIdx = resPool->getPortIdx(params->fetchMemPort);
232 if (fetchPortIdx == 0) {
Korey Sewell1c8dfd92009-05-12 15:01:13 -0400233 fatal("Unable to find port to fetch instructions from.\n");
Korey Sewell973d8b82009-02-10 15:49:29 -0800234 }
235
236 dataPortIdx = resPool->getPortIdx(params->dataMemPort);
237 if (dataPortIdx == 0) {
Korey Sewell1c8dfd92009-05-12 15:01:13 -0400238 fatal("Unable to find port for data.\n");
Korey Sewell973d8b82009-02-10 15:49:29 -0800239 }
240
Nathan Binkert47877cf2009-05-26 09:23:13 -0700241 for (ThreadID tid = 0; tid < numThreads; ++tid) {
Korey Sewellbadb2382009-09-15 01:44:48 -0400242#if FULL_SYSTEM
243 // SMT is not supported in FS mode yet.
Korey Sewellf09f84d2009-10-01 09:35:06 -0400244 assert(numThreads == 1);
245 thread[tid] = new Thread(this, 0);
Korey Sewellbadb2382009-09-15 01:44:48 -0400246#else
Nathan Binkert6faf3772009-06-04 23:21:12 -0700247 if (tid < (ThreadID)params->workload.size()) {
Korey Sewell30cd2d22009-03-04 13:17:08 -0500248 DPRINTF(InOrderCPU, "Workload[%i] process is %#x\n",
Korey Sewellf09f84d2009-10-01 09:35:06 -0400249 tid, params->workload[tid]->prog_fname);
250 thread[tid] =
Gabe Blackc9a27d82009-07-08 23:02:22 -0700251 new Thread(this, tid, params->workload[tid]);
Korey Sewell973d8b82009-02-10 15:49:29 -0800252 } else {
253 //Allocate Empty thread so M5 can use later
254 //when scheduling threads to CPU
Korey Sewell30cd2d22009-03-04 13:17:08 -0500255 Process* dummy_proc = params->workload[0];
Korey Sewellf09f84d2009-10-01 09:35:06 -0400256 thread[tid] = new Thread(this, tid, dummy_proc);
Korey Sewell973d8b82009-02-10 15:49:29 -0800257 }
Korey Sewellbadb2382009-09-15 01:44:48 -0400258#endif
Korey Sewell973d8b82009-02-10 15:49:29 -0800259
260 // Setup the TC that will serve as the interface to the threads/CPU.
261 InOrderThreadContext *tc = new InOrderThreadContext;
262 tc->cpu = this;
Nathan Binkert47877cf2009-05-26 09:23:13 -0700263 tc->thread = thread[tid];
Korey Sewell973d8b82009-02-10 15:49:29 -0800264
265 // Give the thread the TC.
Nathan Binkert47877cf2009-05-26 09:23:13 -0700266 thread[tid]->tc = tc;
267 thread[tid]->setFuncExeInst(0);
268 globalSeqNum[tid] = 1;
Korey Sewell973d8b82009-02-10 15:49:29 -0800269
270 // Add the TC to the CPU's list of TC's.
271 this->threadContexts.push_back(tc);
272 }
273
274 // Initialize TimeBuffer Stage Queues
Korey Sewell973d8b82009-02-10 15:49:29 -0800275 for (int stNum=0; stNum < NumStages - 1; stNum++) {
276 stageQueue[stNum] = new StageQueue(NumStages, NumStages);
Korey Sewell846f9532009-03-04 13:16:49 -0500277 stageQueue[stNum]->id(stNum);
Korey Sewell973d8b82009-02-10 15:49:29 -0800278 }
279
280
281 // Set Up Pipeline Stages
282 for (int stNum=0; stNum < NumStages; stNum++) {
283 if (stNum == 0)
284 pipelineStage[stNum] = new FirstStage(params, stNum);
285 else
286 pipelineStage[stNum] = new PipelineStage(params, stNum);
287
288 pipelineStage[stNum]->setCPU(this);
289 pipelineStage[stNum]->setActiveThreads(&activeThreads);
290 pipelineStage[stNum]->setTimeBuffer(&timeBuffer);
291
292 // Take Care of 1st/Nth stages
293 if (stNum > 0)
294 pipelineStage[stNum]->setPrevStageQueue(stageQueue[stNum - 1]);
Korey Sewellf69b0182009-03-04 13:17:07 -0500295 if (stNum < NumStages - 1)
296 pipelineStage[stNum]->setNextStageQueue(stageQueue[stNum]);
Korey Sewell973d8b82009-02-10 15:49:29 -0800297 }
298
299 // Initialize thread specific variables
Nathan Binkert47877cf2009-05-26 09:23:13 -0700300 for (ThreadID tid = 0; tid < numThreads; tid++) {
Korey Sewell973d8b82009-02-10 15:49:29 -0800301 archRegDepMap[tid].setCPU(this);
302
303 nonSpecInstActive[tid] = false;
304 nonSpecSeqNum[tid] = 0;
305
306 squashSeqNum[tid] = MaxAddr;
307 lastSquashCycle[tid] = 0;
308
Gabe Blacka480ba02009-07-08 23:02:20 -0700309 memset(intRegs[tid], 0, sizeof(intRegs[tid]));
Gabe Black0cb180e2009-07-08 23:02:20 -0700310 memset(floatRegs.i[tid], 0, sizeof(floatRegs.i[tid]));
Gabe Black32daf6f2009-07-08 23:02:20 -0700311 isa[tid].clear();
Korey Sewell973d8b82009-02-10 15:49:29 -0800312
Gabe Black32daf6f2009-07-08 23:02:20 -0700313 isa[tid].expandForMultithreading(numThreads, numVirtProcs);
Korey Sewell973d8b82009-02-10 15:49:29 -0800314 }
315
Korey Sewell973d8b82009-02-10 15:49:29 -0800316 lastRunningCycle = curTick;
317 contextSwitch = false;
318
319 // Define dummy instructions and resource requests to be used.
Korey Sewelld8e09352010-01-31 18:26:03 -0500320 dummyInst = new InOrderDynInst(this, NULL, 0, 0);
Korey Sewell7b3b3622010-01-31 17:18:15 -0500321 dummyReq = new ResourceRequest(resPool->getResource(0), NULL, 0, 0, 0, 0);
Korey Sewell973d8b82009-02-10 15:49:29 -0800322
323 // Reset CPU to reset state.
324#if FULL_SYSTEM
325 Fault resetFault = new ResetFault();
326 resetFault->invoke(tcBase());
327#else
328 reset();
329#endif
330
331 // Schedule First Tick Event, CPU will reschedule itself from here on out.
332 scheduleTickEvent(0);
333}
334
335
336void
337InOrderCPU::regStats()
338{
339 /* Register the Resource Pool's stats here.*/
340 resPool->regStats();
341
Korey Sewell7b3b3622010-01-31 17:18:15 -0500342#ifdef DEBUG
343 maxResReqCount
344 .name(name() + ".maxResReqCount")
345 .desc("Maximum number of live resource requests in CPU")
346 .prereq(maxResReqCount);
347#endif
348
Korey Sewell973d8b82009-02-10 15:49:29 -0800349 /* Register any of the InOrderCPU's stats here.*/
350 timesIdled
351 .name(name() + ".timesIdled")
352 .desc("Number of times that the entire CPU went into an idle state and"
353 " unscheduled itself")
354 .prereq(timesIdled);
355
356 idleCycles
357 .name(name() + ".idleCycles")
358 .desc("Total number of cycles that the CPU has spent unscheduled due "
359 "to idling")
360 .prereq(idleCycles);
361
362 threadCycles
363 .init(numThreads)
364 .name(name() + ".threadCycles")
365 .desc("Total Number of Cycles A Thread Was Active in CPU (Per-Thread)");
366
367 smtCycles
368 .name(name() + ".smtCycles")
Korey Sewell7b3b3622010-01-31 17:18:15 -0500369 .desc("Total number of cycles that the CPU was in SMT-mode");
Korey Sewell973d8b82009-02-10 15:49:29 -0800370
371 committedInsts
372 .init(numThreads)
373 .name(name() + ".committedInsts")
374 .desc("Number of Instructions Simulated (Per-Thread)");
375
376 smtCommittedInsts
377 .init(numThreads)
378 .name(name() + ".smtCommittedInsts")
379 .desc("Number of SMT Instructions Simulated (Per-Thread)");
380
381 totalCommittedInsts
382 .name(name() + ".committedInsts_total")
383 .desc("Number of Instructions Simulated (Total)");
384
385 cpi
386 .name(name() + ".cpi")
387 .desc("CPI: Cycles Per Instruction (Per-Thread)")
388 .precision(6);
389 cpi = threadCycles / committedInsts;
390
391 smtCpi
392 .name(name() + ".smt_cpi")
393 .desc("CPI: Total SMT-CPI")
394 .precision(6);
395 smtCpi = smtCycles / smtCommittedInsts;
396
397 totalCpi
398 .name(name() + ".cpi_total")
399 .desc("CPI: Total CPI of All Threads")
400 .precision(6);
Korey Sewelle4aa4ca2009-03-04 13:16:48 -0500401 totalCpi = numCycles / totalCommittedInsts;
Korey Sewell973d8b82009-02-10 15:49:29 -0800402
403 ipc
404 .name(name() + ".ipc")
405 .desc("IPC: Instructions Per Cycle (Per-Thread)")
406 .precision(6);
407 ipc = committedInsts / threadCycles;
408
409 smtIpc
410 .name(name() + ".smt_ipc")
411 .desc("IPC: Total SMT-IPC")
412 .precision(6);
413 smtIpc = smtCommittedInsts / smtCycles;
414
415 totalIpc
416 .name(name() + ".ipc_total")
417 .desc("IPC: Total IPC of All Threads")
418 .precision(6);
Korey Sewelle4aa4ca2009-03-04 13:16:48 -0500419 totalIpc = totalCommittedInsts / numCycles;
Korey Sewell973d8b82009-02-10 15:49:29 -0800420
421 BaseCPU::regStats();
422}
423
424
425void
426InOrderCPU::tick()
427{
428 DPRINTF(InOrderCPU, "\n\nInOrderCPU: Ticking main, InOrderCPU.\n");
429
430 ++numCycles;
431
432 //Tick each of the stages
433 for (int stNum=NumStages - 1; stNum >= 0 ; stNum--) {
434 pipelineStage[stNum]->tick();
435 }
436
437 // Now advance the time buffers one tick
438 timeBuffer.advance();
439 for (int sqNum=0; sqNum < NumStages - 1; sqNum++) {
440 stageQueue[sqNum]->advance();
441 }
442 activityRec.advance();
443
444 // Any squashed requests, events, or insts then remove them now
445 cleanUpRemovedReqs();
446 cleanUpRemovedEvents();
447 cleanUpRemovedInsts();
448
449 // Re-schedule CPU for this cycle
450 if (!tickEvent.scheduled()) {
451 if (_status == SwitchedOut) {
452 // increment stat
453 lastRunningCycle = curTick;
454 } else if (!activityRec.active()) {
455 DPRINTF(InOrderCPU, "sleeping CPU.\n");
456 lastRunningCycle = curTick;
457 timesIdled++;
458 } else {
459 //Tick next_tick = curTick + cycles(1);
460 //tickEvent.schedule(next_tick);
461 mainEventQueue.schedule(&tickEvent, nextCycle(curTick + 1));
Korey Sewell7b3b3622010-01-31 17:18:15 -0500462 DPRINTF(InOrderCPU, "Scheduled CPU for next tick @ %i.\n",
463 nextCycle(curTick + 1));
Korey Sewell973d8b82009-02-10 15:49:29 -0800464 }
465 }
466
467 tickThreadStats();
468 updateThreadPriority();
469}
470
471
472void
473InOrderCPU::init()
474{
475 if (!deferRegistration) {
476 registerThreadContexts();
477 }
478
479 // Set inSyscall so that the CPU doesn't squash when initially
480 // setting up registers.
Nathan Binkert47877cf2009-05-26 09:23:13 -0700481 for (ThreadID tid = 0; tid < numThreads; ++tid)
482 thread[tid]->inSyscall = true;
Korey Sewell973d8b82009-02-10 15:49:29 -0800483
Steve Reinhardt14808ec2009-04-17 16:54:58 -0700484#if FULL_SYSTEM
Nathan Binkert47877cf2009-05-26 09:23:13 -0700485 for (ThreadID tid = 0; tid < numThreads; tid++) {
Steve Reinhardt14808ec2009-04-17 16:54:58 -0700486 ThreadContext *src_tc = threadContexts[tid];
487 TheISA::initCPU(src_tc, src_tc->contextId());
Korey Sewell973d8b82009-02-10 15:49:29 -0800488 }
Steve Reinhardt14808ec2009-04-17 16:54:58 -0700489#endif
Korey Sewell973d8b82009-02-10 15:49:29 -0800490
491 // Clear inSyscall.
Nathan Binkert47877cf2009-05-26 09:23:13 -0700492 for (ThreadID tid = 0; tid < numThreads; ++tid)
493 thread[tid]->inSyscall = false;
Korey Sewell973d8b82009-02-10 15:49:29 -0800494
495 // Call Initializiation Routine for Resource Pool
496 resPool->init();
497}
498
499void
Korey Sewell973d8b82009-02-10 15:49:29 -0800500InOrderCPU::reset()
501{
Gabe Black32daf6f2009-07-08 23:02:20 -0700502 for (int i = 0; i < numThreads; i++) {
503 isa[i].reset(coreType, numThreads,
504 numVirtProcs, dynamic_cast<BaseCPU*>(this));
505 }
Korey Sewell973d8b82009-02-10 15:49:29 -0800506}
507
508Port*
509InOrderCPU::getPort(const std::string &if_name, int idx)
510{
511 return resPool->getPort(if_name, idx);
512}
513
Korey Sewellbadb2382009-09-15 01:44:48 -0400514#if FULL_SYSTEM
515Fault
516InOrderCPU::hwrei(ThreadID tid)
517{
518 panic("hwrei: Unimplemented");
519
520 return NoFault;
521}
522
523
524bool
525InOrderCPU::simPalCheck(int palFunc, ThreadID tid)
526{
527 panic("simPalCheck: Unimplemented");
528
529 return true;
530}
531
532
533Fault
534InOrderCPU::getInterrupts()
535{
536 // Check if there are any outstanding interrupts
537 return this->interrupts->getInterrupt(this->threadContexts[0]);
538}
539
540
541void
542InOrderCPU::processInterrupts(Fault interrupt)
543{
544 // Check for interrupts here. For now can copy the code that
545 // exists within isa_fullsys_traits.hh. Also assume that thread 0
546 // is the one that handles the interrupts.
547 // @todo: Possibly consolidate the interrupt checking code.
548 // @todo: Allow other threads to handle interrupts.
549
550 assert(interrupt != NoFault);
551 this->interrupts->updateIntrInfo(this->threadContexts[0]);
552
553 DPRINTF(InOrderCPU, "Interrupt %s being handled\n", interrupt->name());
554 this->trap(interrupt, 0);
555}
556
557
558void
559InOrderCPU::updateMemPorts()
560{
561 // Update all ThreadContext's memory ports (Functional/Virtual
562 // Ports)
563 ThreadID size = thread.size();
564 for (ThreadID i = 0; i < size; ++i)
565 thread[i]->connectMemPorts(thread[i]->getTC());
566}
567#endif
568
Korey Sewell973d8b82009-02-10 15:49:29 -0800569void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700570InOrderCPU::trap(Fault fault, ThreadID tid, int delay)
Korey Sewell973d8b82009-02-10 15:49:29 -0800571{
Korey Sewelldb2b7212009-05-12 15:01:16 -0400572 //@ Squash Pipeline during TRAP
Korey Sewelld8e09352010-01-31 18:26:03 -0500573 scheduleCpuEvent(Trap, fault, tid, dummyInst, delay);
Korey Sewell973d8b82009-02-10 15:49:29 -0800574}
575
576void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700577InOrderCPU::trapCPU(Fault fault, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800578{
579 fault->invoke(tcBase(tid));
580}
581
582void
583InOrderCPU::scheduleCpuEvent(CPUEventType c_event, Fault fault,
Korey Sewelld8e09352010-01-31 18:26:03 -0500584 ThreadID tid, DynInstPtr inst,
585 unsigned delay)
Korey Sewell973d8b82009-02-10 15:49:29 -0800586{
Korey Sewelld8e09352010-01-31 18:26:03 -0500587 CPUEvent *cpu_event = new CPUEvent(this, c_event, fault, tid, inst);
Korey Sewell973d8b82009-02-10 15:49:29 -0800588
589 if (delay >= 0) {
Korey Sewelldb2b7212009-05-12 15:01:16 -0400590 DPRINTF(InOrderCPU, "Scheduling CPU Event (%s) for cycle %i.\n",
Korey Sewell3a057bd2009-05-12 15:01:16 -0400591 eventNames[c_event], curTick + delay);
Korey Sewell973d8b82009-02-10 15:49:29 -0800592 mainEventQueue.schedule(cpu_event,curTick + delay);
593 } else {
594 cpu_event->process();
595 cpuEventRemoveList.push(cpu_event);
596 }
597
598 // Broadcast event to the Resource Pool
Nathan Binkert47877cf2009-05-26 09:23:13 -0700599 DynInstPtr dummy_inst =
600 new InOrderDynInst(this, NULL, getNextEventNum(), tid);
Korey Sewelld8e09352010-01-31 18:26:03 -0500601 resPool->scheduleEvent(c_event, inst, 0, 0, tid);
Korey Sewell973d8b82009-02-10 15:49:29 -0800602}
603
604inline bool
Nathan Binkert47877cf2009-05-26 09:23:13 -0700605InOrderCPU::isThreadActive(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800606{
Nathan Binkert47877cf2009-05-26 09:23:13 -0700607 list<ThreadID>::iterator isActive =
608 std::find(activeThreads.begin(), activeThreads.end(), tid);
Korey Sewell973d8b82009-02-10 15:49:29 -0800609
610 return (isActive != activeThreads.end());
611}
612
613
614void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700615InOrderCPU::activateThread(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800616{
617 if (!isThreadActive(tid)) {
Nathan Binkert47877cf2009-05-26 09:23:13 -0700618 DPRINTF(InOrderCPU,
619 "Adding Thread %i to active threads list in CPU.\n", tid);
Korey Sewell973d8b82009-02-10 15:49:29 -0800620 activeThreads.push_back(tid);
621
622 wakeCPU();
623 }
624}
625
626void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700627InOrderCPU::deactivateThread(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800628{
629 DPRINTF(InOrderCPU, "[tid:%i]: Calling deactivate thread.\n", tid);
630
631 if (isThreadActive(tid)) {
632 DPRINTF(InOrderCPU,"[tid:%i]: Removing from active threads list\n",
633 tid);
Nathan Binkert47877cf2009-05-26 09:23:13 -0700634 list<ThreadID>::iterator thread_it =
635 std::find(activeThreads.begin(), activeThreads.end(), tid);
Korey Sewell973d8b82009-02-10 15:49:29 -0800636
637 removePipelineStalls(*thread_it);
638
639 //@TODO: change stage status' to Idle?
640
641 activeThreads.erase(thread_it);
642 }
643}
644
645void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700646InOrderCPU::removePipelineStalls(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800647{
648 DPRINTF(InOrderCPU,"[tid:%i]: Removing all pipeline stalls\n",
649 tid);
650
651 for (int stNum = 0; stNum < NumStages ; stNum++) {
652 pipelineStage[stNum]->removeStalls(tid);
653 }
654
655}
656bool
Nathan Binkert47877cf2009-05-26 09:23:13 -0700657InOrderCPU::isThreadInCPU(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800658{
Nathan Binkert47877cf2009-05-26 09:23:13 -0700659 list<ThreadID>::iterator isCurrent =
660 std::find(currentThreads.begin(), currentThreads.end(), tid);
Korey Sewell973d8b82009-02-10 15:49:29 -0800661
662 return (isCurrent != currentThreads.end());
663}
664
665void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700666InOrderCPU::addToCurrentThreads(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800667{
668 if (!isThreadInCPU(tid)) {
Korey Sewell7b3b3622010-01-31 17:18:15 -0500669 DPRINTF(InOrderCPU, "Adding Thread %i to current threads list in CPU."
670 "\n", tid);
Korey Sewell973d8b82009-02-10 15:49:29 -0800671 currentThreads.push_back(tid);
672 }
673}
674
675void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700676InOrderCPU::removeFromCurrentThreads(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800677{
678 if (isThreadInCPU(tid)) {
Nathan Binkert47877cf2009-05-26 09:23:13 -0700679 DPRINTF(InOrderCPU,
680 "Adding Thread %i to current threads list in CPU.\n", tid);
681 list<ThreadID>::iterator isCurrent =
682 std::find(currentThreads.begin(), currentThreads.end(), tid);
Korey Sewell973d8b82009-02-10 15:49:29 -0800683 currentThreads.erase(isCurrent);
684 }
685}
686
687bool
Nathan Binkert47877cf2009-05-26 09:23:13 -0700688InOrderCPU::isThreadSuspended(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800689{
Nathan Binkert47877cf2009-05-26 09:23:13 -0700690 list<ThreadID>::iterator isSuspended =
691 std::find(suspendedThreads.begin(), suspendedThreads.end(), tid);
Korey Sewell973d8b82009-02-10 15:49:29 -0800692
693 return (isSuspended!= suspendedThreads.end());
694}
695
696void
697InOrderCPU::enableVirtProcElement(unsigned vpe)
698{
699 DPRINTF(InOrderCPU, "[vpe:%i]: Scheduling "
700 "Enabling of concurrent virtual processor execution",
701 vpe);
702
Korey Sewelld8e09352010-01-31 18:26:03 -0500703 scheduleCpuEvent(EnableVPEs, NoFault, 0/*tid*/, dummyInst);
Korey Sewell973d8b82009-02-10 15:49:29 -0800704}
705
706void
707InOrderCPU::enableVPEs(unsigned vpe)
708{
709 DPRINTF(InOrderCPU, "[vpe:%i]: Enabling Concurrent Execution "
710 "virtual processors %i", vpe);
711
Nathan Binkert47877cf2009-05-26 09:23:13 -0700712 list<ThreadID>::iterator thread_it = currentThreads.begin();
Korey Sewell973d8b82009-02-10 15:49:29 -0800713
714 while (thread_it != currentThreads.end()) {
715 if (!isThreadSuspended(*thread_it)) {
716 activateThread(*thread_it);
717 }
718 thread_it++;
719 }
720}
721
722void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700723InOrderCPU::disableVirtProcElement(ThreadID tid, unsigned vpe)
Korey Sewell973d8b82009-02-10 15:49:29 -0800724{
725 DPRINTF(InOrderCPU, "[vpe:%i]: Scheduling "
726 "Disabling of concurrent virtual processor execution",
727 vpe);
728
Korey Sewelld8e09352010-01-31 18:26:03 -0500729 scheduleCpuEvent(DisableVPEs, NoFault, 0/*tid*/, dummyInst);
Korey Sewell973d8b82009-02-10 15:49:29 -0800730}
731
732void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700733InOrderCPU::disableVPEs(ThreadID tid, unsigned vpe)
Korey Sewell973d8b82009-02-10 15:49:29 -0800734{
735 DPRINTF(InOrderCPU, "[vpe:%i]: Disabling Concurrent Execution of "
736 "virtual processors %i", vpe);
737
738 unsigned base_vpe = TheISA::getVirtProcNum(tcBase(tid));
739
Nathan Binkert47877cf2009-05-26 09:23:13 -0700740 list<ThreadID>::iterator thread_it = activeThreads.begin();
Korey Sewell973d8b82009-02-10 15:49:29 -0800741
Nathan Binkert47877cf2009-05-26 09:23:13 -0700742 vector<list<ThreadID>::iterator> removeList;
Korey Sewell973d8b82009-02-10 15:49:29 -0800743
744 while (thread_it != activeThreads.end()) {
745 if (base_vpe != vpe) {
746 removeList.push_back(thread_it);
747 }
748 thread_it++;
749 }
750
751 for (int i = 0; i < removeList.size(); i++) {
752 activeThreads.erase(removeList[i]);
753 }
754}
755
756void
757InOrderCPU::enableMultiThreading(unsigned vpe)
758{
759 // Schedule event to take place at end of cycle
760 DPRINTF(InOrderCPU, "[vpe:%i]: Scheduling Enable Multithreading on "
761 "virtual processor %i", vpe);
762
Korey Sewelld8e09352010-01-31 18:26:03 -0500763 scheduleCpuEvent(EnableThreads, NoFault, 0/*tid*/, dummyInst);
Korey Sewell973d8b82009-02-10 15:49:29 -0800764}
765
766void
767InOrderCPU::enableThreads(unsigned vpe)
768{
769 DPRINTF(InOrderCPU, "[vpe:%i]: Enabling Multithreading on "
770 "virtual processor %i", vpe);
771
Nathan Binkert47877cf2009-05-26 09:23:13 -0700772 list<ThreadID>::iterator thread_it = currentThreads.begin();
Korey Sewell973d8b82009-02-10 15:49:29 -0800773
774 while (thread_it != currentThreads.end()) {
775 if (TheISA::getVirtProcNum(tcBase(*thread_it)) == vpe) {
776 if (!isThreadSuspended(*thread_it)) {
777 activateThread(*thread_it);
778 }
779 }
780 thread_it++;
781 }
782}
783void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700784InOrderCPU::disableMultiThreading(ThreadID tid, unsigned vpe)
Korey Sewell973d8b82009-02-10 15:49:29 -0800785{
786 // Schedule event to take place at end of cycle
787 DPRINTF(InOrderCPU, "[tid:%i]: Scheduling Disable Multithreading on "
788 "virtual processor %i", tid, vpe);
789
Korey Sewelld8e09352010-01-31 18:26:03 -0500790 scheduleCpuEvent(DisableThreads, NoFault, tid, dummyInst);
Korey Sewell973d8b82009-02-10 15:49:29 -0800791}
792
793void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700794InOrderCPU::disableThreads(ThreadID tid, unsigned vpe)
Korey Sewell973d8b82009-02-10 15:49:29 -0800795{
796 DPRINTF(InOrderCPU, "[tid:%i]: Disabling Multithreading on "
797 "virtual processor %i", tid, vpe);
798
Nathan Binkert47877cf2009-05-26 09:23:13 -0700799 list<ThreadID>::iterator thread_it = activeThreads.begin();
Korey Sewell973d8b82009-02-10 15:49:29 -0800800
Nathan Binkert47877cf2009-05-26 09:23:13 -0700801 vector<list<ThreadID>::iterator> removeList;
Korey Sewell973d8b82009-02-10 15:49:29 -0800802
803 while (thread_it != activeThreads.end()) {
804 if (TheISA::getVirtProcNum(tcBase(*thread_it)) == vpe) {
805 removeList.push_back(thread_it);
806 }
807 thread_it++;
808 }
809
810 for (int i = 0; i < removeList.size(); i++) {
811 activeThreads.erase(removeList[i]);
812 }
813}
814
815void
816InOrderCPU::updateThreadPriority()
817{
818 if (activeThreads.size() > 1)
819 {
820 //DEFAULT TO ROUND ROBIN SCHEME
821 //e.g. Move highest priority to end of thread list
Nathan Binkert47877cf2009-05-26 09:23:13 -0700822 list<ThreadID>::iterator list_begin = activeThreads.begin();
823 list<ThreadID>::iterator list_end = activeThreads.end();
Korey Sewell973d8b82009-02-10 15:49:29 -0800824
825 unsigned high_thread = *list_begin;
826
827 activeThreads.erase(list_begin);
828
829 activeThreads.push_back(high_thread);
830 }
831}
832
833inline void
834InOrderCPU::tickThreadStats()
835{
836 /** Keep track of cycles that each thread is active */
Nathan Binkert47877cf2009-05-26 09:23:13 -0700837 list<ThreadID>::iterator thread_it = activeThreads.begin();
Korey Sewell973d8b82009-02-10 15:49:29 -0800838 while (thread_it != activeThreads.end()) {
839 threadCycles[*thread_it]++;
840 thread_it++;
841 }
842
843 // Keep track of cycles where SMT is active
844 if (activeThreads.size() > 1) {
845 smtCycles++;
846 }
847}
848
849void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700850InOrderCPU::activateContext(ThreadID tid, int delay)
Korey Sewell973d8b82009-02-10 15:49:29 -0800851{
852 DPRINTF(InOrderCPU,"[tid:%i]: Activating ...\n", tid);
853
Korey Sewelld8e09352010-01-31 18:26:03 -0500854 scheduleCpuEvent(ActivateThread, NoFault, tid, dummyInst, delay);
Korey Sewell973d8b82009-02-10 15:49:29 -0800855
856 // Be sure to signal that there's some activity so the CPU doesn't
857 // deschedule itself.
858 activityRec.activity();
859
860 _status = Running;
861}
862
863
864void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700865InOrderCPU::suspendContext(ThreadID tid, int delay)
Korey Sewell973d8b82009-02-10 15:49:29 -0800866{
Korey Sewelld8e09352010-01-31 18:26:03 -0500867 scheduleCpuEvent(SuspendThread, NoFault, tid, dummyInst, delay);
Korey Sewell973d8b82009-02-10 15:49:29 -0800868 //_status = Idle;
869}
870
871void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700872InOrderCPU::suspendThread(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800873{
874 DPRINTF(InOrderCPU,"[tid: %i]: Suspended ...\n", tid);
875 deactivateThread(tid);
876}
877
878void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700879InOrderCPU::deallocateContext(ThreadID tid, int delay)
Korey Sewell973d8b82009-02-10 15:49:29 -0800880{
Korey Sewelld8e09352010-01-31 18:26:03 -0500881 scheduleCpuEvent(DeallocateThread, NoFault, tid, dummyInst, delay);
Korey Sewell973d8b82009-02-10 15:49:29 -0800882}
883
884void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700885InOrderCPU::deallocateThread(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800886{
887 DPRINTF(InOrderCPU,"[tid:%i]: Deallocating ...", tid);
888
Korey Sewell973d8b82009-02-10 15:49:29 -0800889 removeFromCurrentThreads(tid);
890
891 deactivateThread(tid);
892
893 squashThreadInPipeline(tid);
894}
895
896void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700897InOrderCPU::squashThreadInPipeline(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800898{
899 //Squash all instructions in each stage
900 for (int stNum=NumStages - 1; stNum >= 0 ; stNum--) {
901 pipelineStage[stNum]->squash(0 /*seq_num*/, tid);
902 }
903}
904
905void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700906InOrderCPU::haltContext(ThreadID tid, int delay)
Korey Sewell973d8b82009-02-10 15:49:29 -0800907{
908 DPRINTF(InOrderCPU, "[tid:%i]: Halt context called.\n", tid);
909
910 // Halt is same thing as deallocate for now
911 // @TODO: Differentiate between halt & deallocate in the CPU
912 // model
913 deallocateContext(tid, delay);
914}
915
916void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700917InOrderCPU::insertThread(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800918{
919 panic("Unimplemented Function\n.");
920}
921
922void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700923InOrderCPU::removeThread(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800924{
925 DPRINTF(InOrderCPU, "Removing Thread %i from CPU.\n", tid);
926
927 /** Broadcast to CPU resources*/
928}
929
Korey Sewell1c8dfd92009-05-12 15:01:13 -0400930PipelineStage*
931InOrderCPU::getPipeStage(int stage_num)
932{
933 return pipelineStage[stage_num];
934}
935
936
Korey Sewell973d8b82009-02-10 15:49:29 -0800937void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700938InOrderCPU::activateWhenReady(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800939{
940 panic("Unimplemented Function\n.");
941}
942
943
Korey Sewell973d8b82009-02-10 15:49:29 -0800944uint64_t
Nathan Binkert47877cf2009-05-26 09:23:13 -0700945InOrderCPU::readPC(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800946{
947 return PC[tid];
948}
949
950
951void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700952InOrderCPU::setPC(Addr new_PC, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800953{
954 PC[tid] = new_PC;
955}
956
957
958uint64_t
Nathan Binkert47877cf2009-05-26 09:23:13 -0700959InOrderCPU::readNextPC(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800960{
961 return nextPC[tid];
962}
963
964
965void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700966InOrderCPU::setNextPC(uint64_t new_NPC, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800967{
968 nextPC[tid] = new_NPC;
969}
970
971
972uint64_t
Nathan Binkert47877cf2009-05-26 09:23:13 -0700973InOrderCPU::readNextNPC(ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800974{
975 return nextNPC[tid];
976}
977
978
979void
Nathan Binkert47877cf2009-05-26 09:23:13 -0700980InOrderCPU::setNextNPC(uint64_t new_NNPC, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800981{
982 nextNPC[tid] = new_NNPC;
983}
984
985uint64_t
Nathan Binkert47877cf2009-05-26 09:23:13 -0700986InOrderCPU::readIntReg(int reg_idx, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800987{
Gabe Blacka480ba02009-07-08 23:02:20 -0700988 return intRegs[tid][reg_idx];
Korey Sewell973d8b82009-02-10 15:49:29 -0800989}
990
991FloatReg
Gabe Black25884a82009-07-08 23:02:20 -0700992InOrderCPU::readFloatReg(int reg_idx, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800993{
Gabe Black0cb180e2009-07-08 23:02:20 -0700994 return floatRegs.f[tid][reg_idx];
Korey Sewell973d8b82009-02-10 15:49:29 -0800995}
996
997FloatRegBits
Gabe Black25884a82009-07-08 23:02:20 -0700998InOrderCPU::readFloatRegBits(int reg_idx, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -0800999{;
Gabe Black0cb180e2009-07-08 23:02:20 -07001000 return floatRegs.i[tid][reg_idx];
Korey Sewell973d8b82009-02-10 15:49:29 -08001001}
1002
1003void
Nathan Binkert47877cf2009-05-26 09:23:13 -07001004InOrderCPU::setIntReg(int reg_idx, uint64_t val, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -08001005{
Gabe Blacka480ba02009-07-08 23:02:20 -07001006 intRegs[tid][reg_idx] = val;
Korey Sewell973d8b82009-02-10 15:49:29 -08001007}
1008
1009
1010void
Gabe Black25884a82009-07-08 23:02:20 -07001011InOrderCPU::setFloatReg(int reg_idx, FloatReg val, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -08001012{
Gabe Black0cb180e2009-07-08 23:02:20 -07001013 floatRegs.f[tid][reg_idx] = val;
Korey Sewell973d8b82009-02-10 15:49:29 -08001014}
1015
1016
1017void
Gabe Black25884a82009-07-08 23:02:20 -07001018InOrderCPU::setFloatRegBits(int reg_idx, FloatRegBits val, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -08001019{
Gabe Black0cb180e2009-07-08 23:02:20 -07001020 floatRegs.i[tid][reg_idx] = val;
Korey Sewell973d8b82009-02-10 15:49:29 -08001021}
1022
1023uint64_t
Nathan Binkert47877cf2009-05-26 09:23:13 -07001024InOrderCPU::readRegOtherThread(unsigned reg_idx, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -08001025{
1026 // If Default value is set, then retrieve target thread
Nathan Binkert47877cf2009-05-26 09:23:13 -07001027 if (tid == InvalidThreadID) {
Korey Sewell973d8b82009-02-10 15:49:29 -08001028 tid = TheISA::getTargetThread(tcBase(tid));
1029 }
1030
Korey Sewell7b3b3622010-01-31 17:18:15 -05001031 if (reg_idx < FP_Base_DepTag) {
1032 // Integer Register File
Korey Sewell973d8b82009-02-10 15:49:29 -08001033 return readIntReg(reg_idx, tid);
Korey Sewell7b3b3622010-01-31 17:18:15 -05001034 } else if (reg_idx < Ctrl_Base_DepTag) {
1035 // Float Register File
Korey Sewell973d8b82009-02-10 15:49:29 -08001036 reg_idx -= FP_Base_DepTag;
1037 return readFloatRegBits(reg_idx, tid);
1038 } else {
1039 reg_idx -= Ctrl_Base_DepTag;
1040 return readMiscReg(reg_idx, tid); // Misc. Register File
1041 }
1042}
1043void
Nathan Binkert47877cf2009-05-26 09:23:13 -07001044InOrderCPU::setRegOtherThread(unsigned reg_idx, const MiscReg &val,
1045 ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -08001046{
1047 // If Default value is set, then retrieve target thread
Nathan Binkert47877cf2009-05-26 09:23:13 -07001048 if (tid == InvalidThreadID) {
Korey Sewell973d8b82009-02-10 15:49:29 -08001049 tid = TheISA::getTargetThread(tcBase(tid));
1050 }
1051
1052 if (reg_idx < FP_Base_DepTag) { // Integer Register File
1053 setIntReg(reg_idx, val, tid);
1054 } else if (reg_idx < Ctrl_Base_DepTag) { // Float Register File
1055 reg_idx -= FP_Base_DepTag;
1056 setFloatRegBits(reg_idx, val, tid);
1057 } else {
1058 reg_idx -= Ctrl_Base_DepTag;
1059 setMiscReg(reg_idx, val, tid); // Misc. Register File
1060 }
1061}
1062
1063MiscReg
Nathan Binkert47877cf2009-05-26 09:23:13 -07001064InOrderCPU::readMiscRegNoEffect(int misc_reg, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -08001065{
Gabe Black32daf6f2009-07-08 23:02:20 -07001066 return isa[tid].readMiscRegNoEffect(misc_reg);
Korey Sewell973d8b82009-02-10 15:49:29 -08001067}
1068
1069MiscReg
Nathan Binkert47877cf2009-05-26 09:23:13 -07001070InOrderCPU::readMiscReg(int misc_reg, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -08001071{
Gabe Black32daf6f2009-07-08 23:02:20 -07001072 return isa[tid].readMiscReg(misc_reg, tcBase(tid));
Korey Sewell973d8b82009-02-10 15:49:29 -08001073}
1074
1075void
Nathan Binkert47877cf2009-05-26 09:23:13 -07001076InOrderCPU::setMiscRegNoEffect(int misc_reg, const MiscReg &val, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -08001077{
Gabe Black32daf6f2009-07-08 23:02:20 -07001078 isa[tid].setMiscRegNoEffect(misc_reg, val);
Korey Sewell973d8b82009-02-10 15:49:29 -08001079}
1080
1081void
Nathan Binkert47877cf2009-05-26 09:23:13 -07001082InOrderCPU::setMiscReg(int misc_reg, const MiscReg &val, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -08001083{
Gabe Black32daf6f2009-07-08 23:02:20 -07001084 isa[tid].setMiscReg(misc_reg, val, tcBase(tid));
Korey Sewell973d8b82009-02-10 15:49:29 -08001085}
1086
1087
1088InOrderCPU::ListIt
1089InOrderCPU::addInst(DynInstPtr &inst)
1090{
Nathan Binkert47877cf2009-05-26 09:23:13 -07001091 ThreadID tid = inst->readTid();
Korey Sewell973d8b82009-02-10 15:49:29 -08001092
1093 instList[tid].push_back(inst);
1094
1095 return --(instList[tid].end());
1096}
1097
1098void
Nathan Binkert47877cf2009-05-26 09:23:13 -07001099InOrderCPU::instDone(DynInstPtr inst, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -08001100{
Korey Sewell7b3b3622010-01-31 17:18:15 -05001101 // Set the CPU's PCs - This contributes to the precise state of the CPU
1102 // which can be used when restoring a thread to the CPU after a fork or
1103 // after an exception
1104 // =================
1105 // @TODO: Set-Up Grad-Info/Committed-Info to let ThreadState know if
1106 // it's a branch or not
Korey Sewell973d8b82009-02-10 15:49:29 -08001107 setPC(inst->readPC(), tid);
1108 setNextPC(inst->readNextPC(), tid);
1109 setNextNPC(inst->readNextNPC(), tid);
1110
1111 // Finalize Trace Data For Instruction
1112 if (inst->traceData) {
1113 //inst->traceData->setCycle(curTick);
1114 inst->traceData->setFetchSeq(inst->seqNum);
1115 //inst->traceData->setCPSeq(cpu->tcBase(tid)->numInst);
1116 inst->traceData->dump();
1117 delete inst->traceData;
1118 inst->traceData = NULL;
1119 }
1120
1121 // Set Last Graduated Instruction In Thread State
1122 //thread[tid]->lastGradInst = inst;
1123
1124 // Increment thread-state's instruction count
1125 thread[tid]->numInst++;
1126
1127 // Increment thread-state's instruction stats
1128 thread[tid]->numInsts++;
1129
1130 // Count committed insts per thread stats
1131 committedInsts[tid]++;
1132
1133 // Count total insts committed stat
1134 totalCommittedInsts++;
1135
1136 // Count SMT-committed insts per thread stat
1137 if (numActiveThreads() > 1) {
1138 smtCommittedInsts[tid]++;
1139 }
1140
1141 // Check for instruction-count-based events.
1142 comInstEventQueue[tid]->serviceEvents(thread[tid]->numInst);
1143
1144 // Broadcast to other resources an instruction
1145 // has been completed
Korey Sewell7b3b3622010-01-31 17:18:15 -05001146 resPool->scheduleEvent((CPUEventType)ResourcePool::InstGraduated, inst,
1147 tid);
Korey Sewell973d8b82009-02-10 15:49:29 -08001148
1149 // Finally, remove instruction from CPU
1150 removeInst(inst);
1151}
1152
1153void
1154InOrderCPU::addToRemoveList(DynInstPtr &inst)
1155{
1156 removeInstsThisCycle = true;
1157
1158 removeList.push(inst->getInstListIt());
1159}
1160
1161void
1162InOrderCPU::removeInst(DynInstPtr &inst)
1163{
1164 DPRINTF(InOrderCPU, "Removing graduated instruction [tid:%i] PC %#x "
1165 "[sn:%lli]\n",
1166 inst->threadNumber, inst->readPC(), inst->seqNum);
1167
1168 removeInstsThisCycle = true;
1169
1170 // Remove the instruction.
1171 removeList.push(inst->getInstListIt());
1172}
1173
1174void
Nathan Binkert47877cf2009-05-26 09:23:13 -07001175InOrderCPU::removeInstsUntil(const InstSeqNum &seq_num, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -08001176{
1177 //assert(!instList[tid].empty());
1178
1179 removeInstsThisCycle = true;
1180
1181 ListIt inst_iter = instList[tid].end();
1182
1183 inst_iter--;
1184
1185 DPRINTF(InOrderCPU, "Deleting instructions from CPU instruction "
1186 "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
1187 tid, seq_num, (*inst_iter)->seqNum);
1188
1189 while ((*inst_iter)->seqNum > seq_num) {
1190
1191 bool break_loop = (inst_iter == instList[tid].begin());
1192
1193 squashInstIt(inst_iter, tid);
1194
1195 inst_iter--;
1196
1197 if (break_loop)
1198 break;
1199 }
1200}
1201
1202
1203inline void
Nathan Binkert47877cf2009-05-26 09:23:13 -07001204InOrderCPU::squashInstIt(const ListIt &instIt, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -08001205{
1206 if ((*instIt)->threadNumber == tid) {
1207 DPRINTF(InOrderCPU, "Squashing instruction, "
1208 "[tid:%i] [sn:%lli] PC %#x\n",
1209 (*instIt)->threadNumber,
1210 (*instIt)->seqNum,
1211 (*instIt)->readPC());
1212
1213 (*instIt)->setSquashed();
1214
1215 removeList.push(instIt);
1216 }
1217}
1218
1219
1220void
1221InOrderCPU::cleanUpRemovedInsts()
1222{
1223 while (!removeList.empty()) {
1224 DPRINTF(InOrderCPU, "Removing instruction, "
1225 "[tid:%i] [sn:%lli] PC %#x\n",
1226 (*removeList.front())->threadNumber,
1227 (*removeList.front())->seqNum,
1228 (*removeList.front())->readPC());
1229
1230 DynInstPtr inst = *removeList.front();
Nathan Binkert47877cf2009-05-26 09:23:13 -07001231 ThreadID tid = inst->threadNumber;
Korey Sewell973d8b82009-02-10 15:49:29 -08001232
1233 // Make Sure Resource Schedule Is Emptied Out
1234 ThePipeline::ResSchedule *inst_sched = &inst->resSched;
1235 while (!inst_sched->empty()) {
1236 ThePipeline::ScheduleEntry* sch_entry = inst_sched->top();
1237 inst_sched->pop();
1238 delete sch_entry;
1239 }
1240
1241 // Remove From Register Dependency Map, If Necessary
1242 archRegDepMap[(*removeList.front())->threadNumber].
1243 remove((*removeList.front()));
1244
1245
1246 // Clear if Non-Speculative
1247 if (inst->staticInst &&
1248 inst->seqNum == nonSpecSeqNum[tid] &&
1249 nonSpecInstActive[tid] == true) {
1250 nonSpecInstActive[tid] = false;
1251 }
1252
1253 instList[tid].erase(removeList.front());
1254
1255 removeList.pop();
1256
1257 DPRINTF(RefCount, "pop from remove list: [sn:%i]: Refcount = %i.\n",
1258 inst->seqNum,
1259 0/*inst->curCount()*/);
1260
1261 }
1262
1263 removeInstsThisCycle = false;
1264}
1265
1266void
1267InOrderCPU::cleanUpRemovedReqs()
1268{
1269 while (!reqRemoveList.empty()) {
1270 ResourceRequest *res_req = reqRemoveList.front();
1271
1272 DPRINTF(RefCount, "[tid:%i]: Removing Request, "
1273 "[sn:%lli] [slot:%i] [stage_num:%i] [res:%s] [refcount:%i].\n",
1274 res_req->inst->threadNumber,
1275 res_req->inst->seqNum,
1276 res_req->getSlot(),
1277 res_req->getStageNum(),
1278 res_req->res->name(),
1279 0/*res_req->inst->curCount()*/);
1280
1281 reqRemoveList.pop();
1282
1283 delete res_req;
1284
1285 DPRINTF(RefCount, "after remove request: [sn:%i]: Refcount = %i.\n",
1286 res_req->inst->seqNum,
1287 0/*res_req->inst->curCount()*/);
1288 }
1289}
1290
1291void
1292InOrderCPU::cleanUpRemovedEvents()
1293{
1294 while (!cpuEventRemoveList.empty()) {
1295 Event *cpu_event = cpuEventRemoveList.front();
1296 cpuEventRemoveList.pop();
1297 delete cpu_event;
1298 }
1299}
1300
Korey Sewell973d8b82009-02-10 15:49:29 -08001301
1302void
1303InOrderCPU::dumpInsts()
1304{
1305 int num = 0;
1306
1307 ListIt inst_list_it = instList[0].begin();
1308
1309 cprintf("Dumping Instruction List\n");
1310
1311 while (inst_list_it != instList[0].end()) {
1312 cprintf("Instruction:%i\nPC:%#x\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
1313 "Squashed:%i\n\n",
1314 num, (*inst_list_it)->readPC(), (*inst_list_it)->threadNumber,
1315 (*inst_list_it)->seqNum, (*inst_list_it)->isIssued(),
1316 (*inst_list_it)->isSquashed());
1317 inst_list_it++;
1318 ++num;
1319 }
1320}
Korey Sewell973d8b82009-02-10 15:49:29 -08001321
1322void
1323InOrderCPU::wakeCPU()
1324{
1325 if (/*activityRec.active() || */tickEvent.scheduled()) {
1326 DPRINTF(Activity, "CPU already running.\n");
1327 return;
1328 }
1329
1330 DPRINTF(Activity, "Waking up CPU\n");
1331
Korey Sewell30cd2d22009-03-04 13:17:08 -05001332 //@todo: figure out how to count idleCycles correctly
Korey Sewell973d8b82009-02-10 15:49:29 -08001333 //idleCycles += (curTick - 1) - lastRunningCycle;
1334
1335 mainEventQueue.schedule(&tickEvent, curTick);
1336}
1337
Korey Sewellbadb2382009-09-15 01:44:48 -04001338#if FULL_SYSTEM
1339
1340void
1341InOrderCPU::wakeup()
1342{
1343 if (this->thread[0]->status() != ThreadContext::Suspended)
1344 return;
1345
1346 this->wakeCPU();
1347
1348 DPRINTF(Quiesce, "Suspended Processor woken\n");
1349 this->threadContexts[0]->activate();
1350}
1351#endif
1352
1353#if !FULL_SYSTEM
Korey Sewell973d8b82009-02-10 15:49:29 -08001354void
Nathan Binkert47877cf2009-05-26 09:23:13 -07001355InOrderCPU::syscall(int64_t callnum, ThreadID tid)
Korey Sewell973d8b82009-02-10 15:49:29 -08001356{
1357 DPRINTF(InOrderCPU, "[tid:%i] Executing syscall().\n\n", tid);
1358
1359 DPRINTF(Activity,"Activity: syscall() called.\n");
1360
1361 // Temporarily increase this by one to account for the syscall
1362 // instruction.
1363 ++(this->thread[tid]->funcExeInst);
1364
1365 // Execute the actual syscall.
1366 this->thread[tid]->syscall(callnum);
1367
1368 // Decrease funcExeInst by one as the normal commit will handle
1369 // incrementing it.
1370 --(this->thread[tid]->funcExeInst);
1371
1372 // Clear Non-Speculative Block Variable
1373 nonSpecInstActive[tid] = false;
1374}
Korey Sewellbadb2382009-09-15 01:44:48 -04001375#endif
Korey Sewell973d8b82009-02-10 15:49:29 -08001376
Korey Sewell1c7e9882009-05-12 15:01:15 -04001377void
1378InOrderCPU::prefetch(DynInstPtr inst)
1379{
1380 Resource *mem_res = resPool->getResource(dataPortIdx);
1381 return mem_res->prefetch(inst);
1382}
1383
1384void
1385InOrderCPU::writeHint(DynInstPtr inst)
1386{
1387 Resource *mem_res = resPool->getResource(dataPortIdx);
1388 return mem_res->writeHint(inst);
1389}
1390
1391
Korey Sewell5127ea22009-05-12 15:01:14 -04001392TheISA::TLB*
Korey Sewell1c8dfd92009-05-12 15:01:13 -04001393InOrderCPU::getITBPtr()
1394{
Korey Sewelldb2b7212009-05-12 15:01:16 -04001395 CacheUnit *itb_res =
1396 dynamic_cast<CacheUnit*>(resPool->getResource(fetchPortIdx));
Korey Sewell5127ea22009-05-12 15:01:14 -04001397 return itb_res->tlb();
Korey Sewell1c8dfd92009-05-12 15:01:13 -04001398}
1399
1400
Korey Sewell5127ea22009-05-12 15:01:14 -04001401TheISA::TLB*
Korey Sewell1c8dfd92009-05-12 15:01:13 -04001402InOrderCPU::getDTBPtr()
1403{
Korey Sewelldb2b7212009-05-12 15:01:16 -04001404 CacheUnit *dtb_res =
1405 dynamic_cast<CacheUnit*>(resPool->getResource(dataPortIdx));
Korey Sewell5127ea22009-05-12 15:01:14 -04001406 return dtb_res->tlb();
Korey Sewell1c8dfd92009-05-12 15:01:13 -04001407}
Korey Sewelldb2b7212009-05-12 15:01:16 -04001408
1409template <class T>
1410Fault
1411InOrderCPU::read(DynInstPtr inst, Addr addr, T &data, unsigned flags)
1412{
1413 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1414 // you want to run w/out caches?
Korey Sewell7b3b3622010-01-31 17:18:15 -05001415 CacheUnit *cache_res =
1416 dynamic_cast<CacheUnit*>(resPool->getResource(dataPortIdx));
Korey Sewelldb2b7212009-05-12 15:01:16 -04001417
1418 return cache_res->read(inst, addr, data, flags);
1419}
1420
1421#ifndef DOXYGEN_SHOULD_SKIP_THIS
1422
1423template
1424Fault
1425InOrderCPU::read(DynInstPtr inst, Addr addr, Twin32_t &data, unsigned flags);
1426
1427template
1428Fault
1429InOrderCPU::read(DynInstPtr inst, Addr addr, Twin64_t &data, unsigned flags);
1430
1431template
1432Fault
1433InOrderCPU::read(DynInstPtr inst, Addr addr, uint64_t &data, unsigned flags);
1434
1435template
1436Fault
1437InOrderCPU::read(DynInstPtr inst, Addr addr, uint32_t &data, unsigned flags);
1438
1439template
1440Fault
1441InOrderCPU::read(DynInstPtr inst, Addr addr, uint16_t &data, unsigned flags);
1442
1443template
1444Fault
1445InOrderCPU::read(DynInstPtr inst, Addr addr, uint8_t &data, unsigned flags);
1446
1447#endif //DOXYGEN_SHOULD_SKIP_THIS
1448
1449template<>
1450Fault
1451InOrderCPU::read(DynInstPtr inst, Addr addr, double &data, unsigned flags)
1452{
1453 return read(inst, addr, *(uint64_t*)&data, flags);
1454}
1455
1456template<>
1457Fault
1458InOrderCPU::read(DynInstPtr inst, Addr addr, float &data, unsigned flags)
1459{
1460 return read(inst, addr, *(uint32_t*)&data, flags);
1461}
1462
1463
1464template<>
1465Fault
1466InOrderCPU::read(DynInstPtr inst, Addr addr, int32_t &data, unsigned flags)
1467{
1468 return read(inst, addr, (uint32_t&)data, flags);
1469}
1470
1471template <class T>
1472Fault
1473InOrderCPU::write(DynInstPtr inst, T data, Addr addr, unsigned flags,
1474 uint64_t *write_res)
1475{
1476 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1477 // you want to run w/out caches?
1478 CacheUnit *cache_res =
1479 dynamic_cast<CacheUnit*>(resPool->getResource(dataPortIdx));
1480 return cache_res->write(inst, data, addr, flags, write_res);
1481}
1482
1483#ifndef DOXYGEN_SHOULD_SKIP_THIS
1484
1485template
1486Fault
1487InOrderCPU::write(DynInstPtr inst, Twin32_t data, Addr addr,
1488 unsigned flags, uint64_t *res);
1489
1490template
1491Fault
1492InOrderCPU::write(DynInstPtr inst, Twin64_t data, Addr addr,
1493 unsigned flags, uint64_t *res);
1494
1495template
1496Fault
1497InOrderCPU::write(DynInstPtr inst, uint64_t data, Addr addr,
1498 unsigned flags, uint64_t *res);
1499
1500template
1501Fault
1502InOrderCPU::write(DynInstPtr inst, uint32_t data, Addr addr,
1503 unsigned flags, uint64_t *res);
1504
1505template
1506Fault
1507InOrderCPU::write(DynInstPtr inst, uint16_t data, Addr addr,
1508 unsigned flags, uint64_t *res);
1509
1510template
1511Fault
1512InOrderCPU::write(DynInstPtr inst, uint8_t data, Addr addr,
1513 unsigned flags, uint64_t *res);
1514
1515#endif //DOXYGEN_SHOULD_SKIP_THIS
1516
1517template<>
1518Fault
Korey Sewell7b3b3622010-01-31 17:18:15 -05001519InOrderCPU::write(DynInstPtr inst, double data, Addr addr, unsigned flags,
1520 uint64_t *res)
Korey Sewelldb2b7212009-05-12 15:01:16 -04001521{
1522 return write(inst, *(uint64_t*)&data, addr, flags, res);
1523}
1524
1525template<>
1526Fault
Korey Sewell7b3b3622010-01-31 17:18:15 -05001527InOrderCPU::write(DynInstPtr inst, float data, Addr addr, unsigned flags,
1528 uint64_t *res)
Korey Sewelldb2b7212009-05-12 15:01:16 -04001529{
1530 return write(inst, *(uint32_t*)&data, addr, flags, res);
1531}
1532
1533
1534template<>
1535Fault
Korey Sewell7b3b3622010-01-31 17:18:15 -05001536InOrderCPU::write(DynInstPtr inst, int32_t data, Addr addr, unsigned flags,
1537 uint64_t *res)
Korey Sewelldb2b7212009-05-12 15:01:16 -04001538{
1539 return write(inst, (uint32_t)data, addr, flags, res);
1540}