inorder: fault handling
[gem5.git] / src / cpu / inorder / resources / fetch_seq_unit.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 "config/the_isa.hh"
33 #include "cpu/inorder/resources/fetch_seq_unit.hh"
34 #include "cpu/inorder/resource_pool.hh"
35
36 using namespace std;
37 using namespace TheISA;
38 using namespace ThePipeline;
39
40 FetchSeqUnit::FetchSeqUnit(std::string res_name, int res_id, int res_width,
41 int res_latency, InOrderCPU *_cpu,
42 ThePipeline::Params *params)
43 : Resource(res_name, res_id, res_width, res_latency, _cpu),
44 instSize(sizeof(MachInst))
45 {
46 for (ThreadID tid = 0; tid < ThePipeline::MaxThreads; tid++) {
47 pcValid[tid] = false;
48 pcBlockStage[tid] = 0;
49
50 squashSeqNum[tid] = (InstSeqNum)-1;
51 lastSquashCycle[tid] = 0;
52 }
53 }
54
55 FetchSeqUnit::~FetchSeqUnit()
56 {
57 delete [] resourceEvent;
58 }
59
60 void
61 FetchSeqUnit::init()
62 {
63 resourceEvent = new FetchSeqEvent[width];
64
65 initSlots();
66 }
67
68 void
69 FetchSeqUnit::execute(int slot_num)
70 {
71 ResourceRequest* fs_req = reqMap[slot_num];
72 DynInstPtr inst = fs_req->inst;
73 ThreadID tid = inst->readTid();
74 int stage_num = fs_req->getStageNum();
75 int seq_num = inst->seqNum;
76
77 DPRINTF(InOrderFetchSeq, "[tid:%i]: Current PC is %s\n", tid,
78 pc[tid]);
79
80 switch (fs_req->cmd)
81 {
82 case AssignNextPC:
83 {
84 if (pcValid[tid]) {
85 inst->pcState(pc[tid]);
86 inst->setMemAddr(pc[tid].instAddr());
87
88 // Advance to next PC (typically PC + 4)
89 pc[tid].advance();
90
91 inst->setSeqNum(cpu->getAndIncrementInstSeq(tid));
92
93 DPRINTF(InOrderFetchSeq, "[tid:%i]: Assigning [sn:%i] to "
94 "PC %s\n", tid, inst->seqNum, inst->pcState());
95
96 fs_req->done();
97 } else {
98 DPRINTF(InOrderStall, "STALL: [tid:%i]: NPC not valid\n", tid);
99 fs_req->setCompleted(false);
100 }
101 }
102 break;
103
104 case UpdateTargetPC:
105 {
106 if (inst->isControl()) {
107 // If it's a return, then we must wait for resolved address.
108 if (inst->isReturn() && !inst->predTaken()) {
109 cpu->pipelineStage[stage_num]->
110 toPrevStages->stageBlock[stage_num][tid] = true;
111 pcValid[tid] = false;
112 pcBlockStage[tid] = stage_num;
113 } else if (inst->isCondDelaySlot() && !inst->predTaken()) {
114 // Not-Taken AND Conditional Control
115 DPRINTF(InOrderFetchSeq, "[tid:%i]: [sn:%i]: [PC:%s] "
116 "Predicted Not-Taken Cond. Delay inst. Skipping "
117 "delay slot and Updating PC to %s\n",
118 tid, inst->seqNum, inst->pcState(),
119 inst->readPredTarg());
120
121 DPRINTF(InOrderFetchSeq, "[tid:%i] Setting up squash to "
122 "start from stage %i, after [sn:%i].\n", tid,
123 stage_num, seq_num);
124
125 inst->bdelaySeqNum = seq_num;
126 inst->squashingStage = stage_num;
127
128 squashAfterInst(inst, stage_num, tid);
129 } else if (!inst->isCondDelaySlot() && !inst->predTaken()) {
130 // Not-Taken Control
131 DPRINTF(InOrderFetchSeq, "[tid:%i]: [sn:%i]: Predicted "
132 "Not-Taken Control "
133 "inst. updating PC to %s\n", tid, inst->seqNum,
134 inst->readPredTarg());
135 #if ISA_HAS_DELAY_SLOT
136 pc[tid] = inst->pcState();
137 advancePC(pc[tid], inst->staticInst);
138 #endif
139 } else if (inst->predTaken()) {
140 // Taken Control
141 #if ISA_HAS_DELAY_SLOT
142 pc[tid] = inst->readPredTarg();
143
144 DPRINTF(InOrderFetchSeq, "[tid:%i]: [sn:%i] Updating delay"
145 " slot target to PC %s\n", tid, inst->seqNum,
146 inst->readPredTarg());
147 inst->bdelaySeqNum = seq_num + 1;
148 #else
149 inst->bdelaySeqNum = seq_num;
150 #endif
151
152 inst->squashingStage = stage_num;
153 DPRINTF(InOrderFetchSeq, "[tid:%i] Setting up squash to "
154 "start from stage %i, after [sn:%i].\n",
155 tid, stage_num, inst->bdelaySeqNum);
156
157 // Do Squashing
158 squashAfterInst(inst, stage_num, tid);
159 }
160 } else {
161 DPRINTF(InOrderFetchSeq, "[tid:%i]: [sn:%i]: Ignoring branch "
162 "target update since then is not a control "
163 "instruction.\n", tid, inst->seqNum);
164 }
165
166 fs_req->done();
167 }
168 break;
169
170 default:
171 fatal("Unrecognized command to %s", resName);
172 }
173 }
174
175 inline void
176 FetchSeqUnit::squashAfterInst(DynInstPtr inst, int stage_num, ThreadID tid)
177 {
178 // Squash In Pipeline Stage
179 cpu->pipelineStage[stage_num]->squashDueToBranch(inst, tid);
180
181 // Squash inside current resource, so if there needs to be fetching on
182 // same cycle the fetch information will be correct.
183
184 // Schedule Squash Through-out Resource Pool
185 cpu->resPool->scheduleEvent(
186 (InOrderCPU::CPUEventType)ResourcePool::SquashAll, inst, 0);
187 }
188
189 void
190 FetchSeqUnit::squash(DynInstPtr inst, int squash_stage,
191 InstSeqNum squash_seq_num, ThreadID tid)
192 {
193 DPRINTF(InOrderFetchSeq, "[tid:%i]: Updating due to squash from stage %i."
194 "\n", tid, squash_stage);
195
196 InstSeqNum done_seq_num = inst->bdelaySeqNum;
197
198 // Handles the case where we are squashing because of something that is
199 // not a branch...like a memory stall
200 TheISA::PCState newPC;
201 if (inst->isControl()) {
202 newPC = inst->readPredTarg();
203 } else {
204 TheISA::PCState thisPC = inst->pcState();
205 assert(inst->staticInst);
206 advancePC(thisPC, inst->staticInst);
207 newPC = thisPC;
208 }
209
210 if (squashSeqNum[tid] <= done_seq_num &&
211 lastSquashCycle[tid] == curTick()) {
212 DPRINTF(InOrderFetchSeq, "[tid:%i]: Ignoring squash from stage %i, "
213 "since there is an outstanding squash that is older.\n",
214 tid, squash_stage);
215 } else {
216 squashSeqNum[tid] = done_seq_num;
217 lastSquashCycle[tid] = curTick();
218
219 // If The very next instruction number is the done seq. num,
220 // then we haven't seen the delay slot yet ... if it isn't
221 // the last done_seq_num then this is the delay slot inst.
222 if (cpu->nextInstSeqNum(tid) != done_seq_num &&
223 !inst->procDelaySlotOnMispred) {
224
225 // Reset PC
226 pc[tid] = newPC;
227 #if ISA_HAS_DELAY_SLOT
228 TheISA::advancePC(pc[tid], inst->staticInst);
229 #endif
230
231 DPRINTF(InOrderFetchSeq, "[tid:%i]: Setting PC to %s.\n",
232 tid, newPC);
233 } else {
234 assert(ISA_HAS_DELAY_SLOT);
235
236 pc[tid] = (inst->procDelaySlotOnMispred) ?
237 inst->branchTarget() : newPC;
238
239 // Reset PC to Delay Slot Instruction
240 if (inst->procDelaySlotOnMispred) {
241 // Reset PC
242 pc[tid] = newPC;
243 }
244
245 }
246
247 // Unblock Any Stages Waiting for this information to be updated ...
248 if (!pcValid[tid]) {
249 cpu->pipelineStage[pcBlockStage[tid]]->
250 toPrevStages->stageUnblock[pcBlockStage[tid]][tid] = true;
251 }
252
253 pcValid[tid] = true;
254 }
255
256 Resource::squash(inst, squash_stage, squash_seq_num, tid);
257 }
258
259 FetchSeqUnit::FetchSeqEvent::FetchSeqEvent()
260 : ResourceEvent()
261 { }
262
263 void
264 FetchSeqUnit::FetchSeqEvent::process()
265 {
266 FetchSeqUnit* fs_res = dynamic_cast<FetchSeqUnit*>(resource);
267 assert(fs_res);
268
269 for (int i = 0; i < MaxThreads; i++) {
270 fs_res->pc[i] = fs_res->cpu->pcState(i);
271 DPRINTF(InOrderFetchSeq, "[tid:%i]: Setting PC: %s.\n",
272 fs_res->pc[i]);
273
274 fs_res->pcValid[i] = true;
275 }
276 }
277
278
279 void
280 FetchSeqUnit::activateThread(ThreadID tid)
281 {
282 pcValid[tid] = true;
283
284 pc[tid] = cpu->pcState(tid);
285
286 cpu->fetchPriorityList.push_back(tid);
287
288 DPRINTF(InOrderFetchSeq, "[tid:%i]: Reading PC: %s.\n",
289 tid, pc[tid]);
290 }
291
292 void
293 FetchSeqUnit::deactivateThread(ThreadID tid)
294 {
295 pcValid[tid] = false;
296 pcBlockStage[tid] = 0;
297
298 squashSeqNum[tid] = (InstSeqNum)-1;
299 lastSquashCycle[tid] = 0;
300
301 list<ThreadID>::iterator thread_it = find(cpu->fetchPriorityList.begin(),
302 cpu->fetchPriorityList.end(),
303 tid);
304
305 if (thread_it != cpu->fetchPriorityList.end())
306 cpu->fetchPriorityList.erase(thread_it);
307 }
308
309 void
310 FetchSeqUnit::suspendThread(ThreadID tid)
311 {
312 deactivateThread(tid);
313 }
314
315 void
316 FetchSeqUnit::updateAfterContextSwitch(DynInstPtr inst, ThreadID tid)
317 {
318 pcValid[tid] = true;
319
320 if (cpu->thread[tid]->lastGradIsBranch) {
321 /** This function assumes that the instruction causing the context
322 * switch was right after the branch. Thus, if it's not, then
323 * we are updating incorrectly here
324 */
325 assert(cpu->nextInstAddr(tid) == inst->instAddr());
326 pc[tid] = cpu->thread[tid]->lastBranchPC;
327 } else {
328 pc[tid] = inst->pcState();
329 }
330 assert(inst->staticInst);
331 advancePC(pc[tid], inst->staticInst);
332
333 DPRINTF(InOrderFetchSeq, "[tid:%i]: Updating PCs due to Context Switch."
334 "Assigning PC: %s.\n", tid, pc[tid]);
335 }