inorder: implement trap handling
[gem5.git] / src / cpu / inorder / resources / graduation_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 "cpu/inorder/resources/graduation_unit.hh"
33 #include "debug/InOrderGraduation.hh"
34
35 using namespace ThePipeline;
36
37 GraduationUnit::GraduationUnit(std::string res_name, int res_id, int res_width,
38 int res_latency, InOrderCPU *_cpu,
39 ThePipeline::Params *params)
40 : Resource(res_name, res_id, res_width, res_latency, _cpu),
41 lastNonSpecTick(0)
42 {
43 for (ThreadID tid = 0; tid < ThePipeline::MaxThreads; tid++) {
44 nonSpecInstActive[tid] = &cpu->nonSpecInstActive[tid];
45 nonSpecSeqNum[tid] = &cpu->nonSpecSeqNum[tid];
46 }
47 }
48
49 void
50 GraduationUnit::execute(int slot_num)
51 {
52 ResourceRequest* grad_req = reqs[slot_num];
53 DynInstPtr inst = reqs[slot_num]->inst;
54 ThreadID tid = inst->readTid();
55 int stage_num = inst->curSkedEntry->stageNum;
56
57 switch (grad_req->cmd)
58 {
59 case GraduateInst:
60 {
61 if (lastNonSpecTick == curTick()) {
62 DPRINTF(InOrderGraduation, "Unable to graduate [sn:%i]. "
63 "Only 1 nonspec inst. per cycle can graduate.\n");
64 grad_req->done(false);
65 return;
66 }
67
68 // Handle Any Faults Before Graduating Instruction
69 if (inst->fault != NoFault) {
70 DPRINTF(Fault, "[sn:%i]: fault %s found for %s\n",
71 inst->seqNum, inst->fault->name(),
72 inst->instName());
73 inst->setSquashInfo(stage_num);
74 setupSquash(inst, stage_num, tid);
75 cpu->trapContext(inst->fault, tid, inst);
76 grad_req->done(false);
77 return;
78 }
79
80 DPRINTF(InOrderGraduation,
81 "[tid:%i] Graduating instruction %s [sn:%i].\n",
82 tid, inst->instName(), inst->seqNum);
83
84 // Release Non-Speculative "Block" on instructions that could not
85 // execute because there was a non-speculative inst. active.
86 // @TODO: Fix this functionality. Probably too conservative.
87 if (inst->isNonSpeculative()) {
88 *nonSpecInstActive[tid] = false;
89 DPRINTF(InOrderGraduation,
90 "[tid:%i] Non-speculative inst [sn:%i] graduated\n",
91 tid, inst->seqNum);
92 lastNonSpecTick = curTick();
93 }
94
95 if (inst->traceData) {
96 inst->traceData->setStageCycle(stage_num, curTick());
97 }
98
99 // Tell CPU that instruction is finished processing
100 cpu->instDone(inst, tid);
101
102 grad_req->done();
103 }
104 break;
105
106 default:
107 fatal("Unrecognized command to %s", resName);
108 }
109
110 }