Merge zizzer:/bk/m5 into zed.eecs.umich.edu:/z/hsul/work/m5
[gem5.git] / cpu / exec_context.cc
1 /*
2 * Copyright (c) 2001-2004 The Regents of The University of Michigan
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
29 #include <string>
30
31 #include "cpu/base_cpu.hh"
32 #include "cpu/exec_context.hh"
33
34 #ifdef FULL_SYSTEM
35 #include "base/cprintf.hh"
36 #include "kern/kernel_stats.hh"
37 #include "sim/serialize.hh"
38 #include "sim/system.hh"
39 #else
40 #include "sim/process.hh"
41 #endif
42
43 using namespace std;
44
45 // constructor
46 #ifdef FULL_SYSTEM
47 ExecContext::ExecContext(BaseCPU *_cpu, int _thread_num, System *_sys,
48 AlphaITB *_itb, AlphaDTB *_dtb,
49 FunctionalMemory *_mem)
50 : _status(ExecContext::Unallocated), cpu(_cpu), thread_num(_thread_num),
51 cpu_id(-1), mem(_mem), itb(_itb), dtb(_dtb), system(_sys),
52 memctrl(_sys->memctrl), physmem(_sys->physmem),
53 kernelBinning(system->kernelBinning), bin(kernelBinning->bin),
54 fnbin(kernelBinning->fnbin), func_exe_inst(0), storeCondFailures(0)
55 {
56 kernelStats = new Kernel::Statistics(this);
57 memset(&regs, 0, sizeof(RegFile));
58 }
59 #else
60 ExecContext::ExecContext(BaseCPU *_cpu, int _thread_num,
61 Process *_process, int _asid)
62 : _status(ExecContext::Unallocated),
63 cpu(_cpu), thread_num(_thread_num), cpu_id(-1),
64 process(_process), mem(process->getMemory()), asid(_asid),
65 func_exe_inst(0), storeCondFailures(0)
66 {
67 memset(&regs, 0, sizeof(RegFile));
68 }
69
70 ExecContext::ExecContext(BaseCPU *_cpu, int _thread_num,
71 FunctionalMemory *_mem, int _asid)
72 : cpu(_cpu), thread_num(_thread_num), process(0), mem(_mem), asid(_asid),
73 func_exe_inst(0), storeCondFailures(0)
74 {
75 memset(&regs, 0, sizeof(RegFile));
76 }
77 #endif
78
79 ExecContext::~ExecContext()
80 {
81 #ifdef FULL_SYSTEM
82 delete kernelStats;
83 #endif
84 }
85
86
87 void
88 ExecContext::takeOverFrom(ExecContext *oldContext)
89 {
90 // some things should already be set up
91 assert(mem == oldContext->mem);
92 #ifdef FULL_SYSTEM
93 assert(system == oldContext->system);
94 #else
95 assert(process == oldContext->process);
96 #endif
97
98 // copy over functional state
99 _status = oldContext->_status;
100 regs = oldContext->regs;
101 cpu_id = oldContext->cpu_id;
102 func_exe_inst = oldContext->func_exe_inst;
103
104 storeCondFailures = 0;
105
106 oldContext->_status = ExecContext::Unallocated;
107 }
108
109 #ifdef FULL_SYSTEM
110 void
111 ExecContext::execute(const StaticInstBase *inst)
112 {
113 assert(kernelStats);
114 system->kernelBinning->execute(this, inst);
115 }
116 #endif
117
118 void
119 ExecContext::serialize(ostream &os)
120 {
121 SERIALIZE_ENUM(_status);
122 regs.serialize(os);
123 // thread_num and cpu_id are deterministic from the config
124 SERIALIZE_SCALAR(func_exe_inst);
125 SERIALIZE_SCALAR(inst);
126
127 #ifdef FULL_SYSTEM
128 kernelStats->serialize(os);
129 #endif
130 }
131
132
133 void
134 ExecContext::unserialize(Checkpoint *cp, const std::string &section)
135 {
136 UNSERIALIZE_ENUM(_status);
137 regs.unserialize(cp, section);
138 // thread_num and cpu_id are deterministic from the config
139 UNSERIALIZE_SCALAR(func_exe_inst);
140 UNSERIALIZE_SCALAR(inst);
141
142 #ifdef FULL_SYSTEM
143 kernelStats->unserialize(cp, section);
144 #endif
145 }
146
147
148 void
149 ExecContext::activate(int delay)
150 {
151 if (status() == Active)
152 return;
153
154 _status = Active;
155 cpu->activateContext(thread_num, delay);
156 }
157
158 void
159 ExecContext::suspend()
160 {
161 if (status() == Suspended)
162 return;
163
164 #ifdef FULL_SYSTEM
165 // Don't change the status from active if there are pending interrupts
166 if (cpu->check_interrupts()) {
167 assert(status() == Active);
168 return;
169 }
170 #endif
171
172 _status = Suspended;
173 cpu->suspendContext(thread_num);
174 }
175
176 void
177 ExecContext::deallocate()
178 {
179 if (status() == Unallocated)
180 return;
181
182 _status = Unallocated;
183 cpu->deallocateContext(thread_num);
184 }
185
186 void
187 ExecContext::halt()
188 {
189 if (status() == Halted)
190 return;
191
192 _status = Halted;
193 cpu->haltContext(thread_num);
194 }
195
196
197 void
198 ExecContext::regStats(const string &name)
199 {
200 #ifdef FULL_SYSTEM
201 kernelStats->regStats(name + ".kern");
202 #endif
203 }
204
205 void
206 ExecContext::trap(Fault fault)
207 {
208 //TheISA::trap(fault); //One possible way to do it...
209
210 /** @todo: Going to hack it for now. Do a true fixup later. */
211 #ifdef FULL_SYSTEM
212 ev5_trap(fault);
213 #else
214 fatal("fault (%d) detected @ PC 0x%08p", fault, readPC());
215 #endif
216 }