Change ExecContext to ThreadContext. This is being renamed to differentiate between...
[gem5.git] / src / cpu / cpu_exec_context.cc
1 /*
2 * Copyright (c) 2001-2006 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 * Authors: Steve Reinhardt
29 * Nathan Binkert
30 * Lisa Hsu
31 * Kevin Lim
32 */
33
34 #include <string>
35
36 #include "arch/isa_traits.hh"
37 #include "cpu/base.hh"
38 #include "cpu/cpu_exec_context.hh"
39 #include "cpu/thread_context.hh"
40
41 #if FULL_SYSTEM
42 #include "base/callback.hh"
43 #include "base/cprintf.hh"
44 #include "base/output.hh"
45 #include "base/trace.hh"
46 #include "cpu/profile.hh"
47 #include "cpu/quiesce_event.hh"
48 #include "kern/kernel_stats.hh"
49 #include "sim/serialize.hh"
50 #include "sim/sim_exit.hh"
51 #include "arch/stacktrace.hh"
52 #else
53 #include "sim/process.hh"
54 #include "sim/system.hh"
55 #include "mem/translating_port.hh"
56 #endif
57
58 using namespace std;
59
60 // constructor
61 #if FULL_SYSTEM
62 CPUExecContext::CPUExecContext(BaseCPU *_cpu, int _thread_num, System *_sys,
63 AlphaITB *_itb, AlphaDTB *_dtb,
64 bool use_kernel_stats)
65 : _status(ThreadContext::Unallocated), cpu(_cpu), thread_num(_thread_num),
66 cpu_id(-1), lastActivate(0), lastSuspend(0), system(_sys), itb(_itb),
67 dtb(_dtb), profile(NULL), func_exe_inst(0), storeCondFailures(0)
68
69 {
70 tc = new ProxyThreadContext<CPUExecContext>(this);
71
72 quiesceEvent = new EndQuiesceEvent(tc);
73
74 regs.clear();
75
76 if (cpu->params->profile) {
77 profile = new FunctionProfile(system->kernelSymtab);
78 Callback *cb =
79 new MakeCallback<CPUExecContext,
80 &CPUExecContext::dumpFuncProfile>(this);
81 registerExitCallback(cb);
82 }
83
84 // let's fill with a dummy node for now so we don't get a segfault
85 // on the first cycle when there's no node available.
86 static ProfileNode dummyNode;
87 profileNode = &dummyNode;
88 profilePC = 3;
89
90
91 if (use_kernel_stats) {
92 kernelStats = new Kernel::Statistics(system);
93 } else {
94 kernelStats = NULL;
95 }
96 Port *mem_port;
97 physPort = new FunctionalPort(csprintf("%s-%d-funcport",
98 cpu->name(), thread_num));
99 mem_port = system->physmem->getPort("functional");
100 mem_port->setPeer(physPort);
101 physPort->setPeer(mem_port);
102
103 virtPort = new VirtualPort(csprintf("%s-%d-vport",
104 cpu->name(), thread_num));
105 mem_port = system->physmem->getPort("functional");
106 mem_port->setPeer(virtPort);
107 virtPort->setPeer(mem_port);
108 }
109 #else
110 CPUExecContext::CPUExecContext(BaseCPU *_cpu, int _thread_num,
111 Process *_process, int _asid, MemObject* memobj)
112 : _status(ThreadContext::Unallocated),
113 cpu(_cpu), thread_num(_thread_num), cpu_id(-1), lastActivate(0),
114 lastSuspend(0), process(_process), asid(_asid),
115 func_exe_inst(0), storeCondFailures(0)
116 {
117 /* Use this port to for syscall emulation writes to memory. */
118 Port *mem_port;
119 port = new TranslatingPort(csprintf("%s-%d-funcport",
120 cpu->name(), thread_num),
121 process->pTable, false);
122 mem_port = memobj->getPort("functional");
123 mem_port->setPeer(port);
124 port->setPeer(mem_port);
125
126 regs.clear();
127 tc = new ProxyThreadContext<CPUExecContext>(this);
128 }
129
130 CPUExecContext::CPUExecContext(RegFile *regFile)
131 : cpu(NULL), thread_num(-1), process(NULL), asid(-1),
132 func_exe_inst(0), storeCondFailures(0)
133 {
134 regs = *regFile;
135 tc = new ProxyThreadContext<CPUExecContext>(this);
136 }
137
138 #endif
139
140 CPUExecContext::~CPUExecContext()
141 {
142 delete tc;
143 }
144
145 #if FULL_SYSTEM
146 void
147 CPUExecContext::dumpFuncProfile()
148 {
149 std::ostream *os = simout.create(csprintf("profile.%s.dat", cpu->name()));
150 profile->dump(tc, *os);
151 }
152
153 void
154 CPUExecContext::profileClear()
155 {
156 if (profile)
157 profile->clear();
158 }
159
160 void
161 CPUExecContext::profileSample()
162 {
163 if (profile)
164 profile->sample(profileNode, profilePC);
165 }
166
167 #endif
168
169 void
170 CPUExecContext::takeOverFrom(ThreadContext *oldContext)
171 {
172 // some things should already be set up
173 #if FULL_SYSTEM
174 assert(system == oldContext->getSystemPtr());
175 #else
176 assert(process == oldContext->getProcessPtr());
177 #endif
178
179 // copy over functional state
180 _status = oldContext->status();
181 copyArchRegs(oldContext);
182 cpu_id = oldContext->readCpuId();
183 #if !FULL_SYSTEM
184 func_exe_inst = oldContext->readFuncExeInst();
185 #else
186 EndQuiesceEvent *quiesce = oldContext->getQuiesceEvent();
187 if (quiesce) {
188 // Point the quiesce event's TC at this TC so that it wakes up
189 // the proper CPU.
190 quiesce->tc = tc;
191 }
192 if (quiesceEvent) {
193 quiesceEvent->tc = tc;
194 }
195 #endif
196
197 storeCondFailures = 0;
198
199 oldContext->setStatus(ThreadContext::Unallocated);
200 }
201
202 void
203 CPUExecContext::serialize(ostream &os)
204 {
205 SERIALIZE_ENUM(_status);
206 regs.serialize(os);
207 // thread_num and cpu_id are deterministic from the config
208 SERIALIZE_SCALAR(func_exe_inst);
209 SERIALIZE_SCALAR(inst);
210
211 #if FULL_SYSTEM
212 Tick quiesceEndTick = 0;
213 if (quiesceEvent->scheduled())
214 quiesceEndTick = quiesceEvent->when();
215 SERIALIZE_SCALAR(quiesceEndTick);
216 if (kernelStats)
217 kernelStats->serialize(os);
218 #endif
219 }
220
221
222 void
223 CPUExecContext::unserialize(Checkpoint *cp, const std::string &section)
224 {
225 UNSERIALIZE_ENUM(_status);
226 regs.unserialize(cp, section);
227 // thread_num and cpu_id are deterministic from the config
228 UNSERIALIZE_SCALAR(func_exe_inst);
229 UNSERIALIZE_SCALAR(inst);
230
231 #if FULL_SYSTEM
232 Tick quiesceEndTick;
233 UNSERIALIZE_SCALAR(quiesceEndTick);
234 if (quiesceEndTick)
235 quiesceEvent->schedule(quiesceEndTick);
236 if (kernelStats)
237 kernelStats->unserialize(cp, section);
238 #endif
239 }
240
241
242 void
243 CPUExecContext::activate(int delay)
244 {
245 if (status() == ThreadContext::Active)
246 return;
247
248 lastActivate = curTick;
249
250 if (status() == ThreadContext::Unallocated) {
251 cpu->activateWhenReady(thread_num);
252 return;
253 }
254
255 _status = ThreadContext::Active;
256
257 // status() == Suspended
258 cpu->activateContext(thread_num, delay);
259 }
260
261 void
262 CPUExecContext::suspend()
263 {
264 if (status() == ThreadContext::Suspended)
265 return;
266
267 lastActivate = curTick;
268 lastSuspend = curTick;
269 /*
270 #if FULL_SYSTEM
271 // Don't change the status from active if there are pending interrupts
272 if (cpu->check_interrupts()) {
273 assert(status() == ThreadContext::Active);
274 return;
275 }
276 #endif
277 */
278 _status = ThreadContext::Suspended;
279 cpu->suspendContext(thread_num);
280 }
281
282 void
283 CPUExecContext::deallocate()
284 {
285 if (status() == ThreadContext::Unallocated)
286 return;
287
288 _status = ThreadContext::Unallocated;
289 cpu->deallocateContext(thread_num);
290 }
291
292 void
293 CPUExecContext::halt()
294 {
295 if (status() == ThreadContext::Halted)
296 return;
297
298 _status = ThreadContext::Halted;
299 cpu->haltContext(thread_num);
300 }
301
302
303 void
304 CPUExecContext::regStats(const string &name)
305 {
306 #if FULL_SYSTEM
307 if (kernelStats)
308 kernelStats->regStats(name + ".kern");
309 #endif
310 }
311
312 void
313 CPUExecContext::copyArchRegs(ThreadContext *src_tc)
314 {
315 TheISA::copyRegs(src_tc, tc);
316 }
317
318 #if FULL_SYSTEM
319 VirtualPort*
320 CPUExecContext::getVirtPort(ThreadContext *src_tc)
321 {
322 if (!src_tc)
323 return virtPort;
324
325 VirtualPort *vp;
326 Port *mem_port;
327
328 vp = new VirtualPort("tc-vport", src_tc);
329 mem_port = system->physmem->getPort("functional");
330 mem_port->setPeer(vp);
331 vp->setPeer(mem_port);
332 return vp;
333 }
334
335 void
336 CPUExecContext::delVirtPort(VirtualPort *vp)
337 {
338 // assert(!vp->nullThreadContext());
339 delete vp->getPeer();
340 delete vp;
341 }
342
343
344 #endif
345