2 * Copyright (c) 2001-2006 The Regents of The University of Michigan
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.
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.
28 * Authors: Steve Reinhardt
36 #include "arch/isa_traits.hh"
37 #include "cpu/base.hh"
38 #include "cpu/cpu_exec_context.hh"
39 #include "cpu/thread_context.hh"
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"
53 #include "sim/process.hh"
54 #include "sim/system.hh"
55 #include "mem/translating_port.hh"
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)
70 tc
= new ProxyThreadContext
<CPUExecContext
>(this);
72 quiesceEvent
= new EndQuiesceEvent(tc
);
76 if (cpu
->params
->profile
) {
77 profile
= new FunctionProfile(system
->kernelSymtab
);
79 new MakeCallback
<CPUExecContext
,
80 &CPUExecContext::dumpFuncProfile
>(this);
81 registerExitCallback(cb
);
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
;
91 if (use_kernel_stats
) {
92 kernelStats
= new Kernel::Statistics(system
);
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
);
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
);
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)
117 /* Use this port to for syscall emulation writes to memory. */
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
);
127 tc
= new ProxyThreadContext
<CPUExecContext
>(this);
130 CPUExecContext::CPUExecContext(RegFile
*regFile
)
131 : cpu(NULL
), thread_num(-1), process(NULL
), asid(-1),
132 func_exe_inst(0), storeCondFailures(0)
135 tc
= new ProxyThreadContext
<CPUExecContext
>(this);
140 CPUExecContext::~CPUExecContext()
147 CPUExecContext::dumpFuncProfile()
149 std::ostream
*os
= simout
.create(csprintf("profile.%s.dat", cpu
->name()));
150 profile
->dump(tc
, *os
);
154 CPUExecContext::profileClear()
161 CPUExecContext::profileSample()
164 profile
->sample(profileNode
, profilePC
);
170 CPUExecContext::takeOverFrom(ThreadContext
*oldContext
)
172 // some things should already be set up
174 assert(system
== oldContext
->getSystemPtr());
176 assert(process
== oldContext
->getProcessPtr());
179 // copy over functional state
180 _status
= oldContext
->status();
181 copyArchRegs(oldContext
);
182 cpu_id
= oldContext
->readCpuId();
184 func_exe_inst
= oldContext
->readFuncExeInst();
186 EndQuiesceEvent
*quiesce
= oldContext
->getQuiesceEvent();
188 // Point the quiesce event's TC at this TC so that it wakes up
193 quiesceEvent
->tc
= tc
;
197 storeCondFailures
= 0;
199 oldContext
->setStatus(ThreadContext::Unallocated
);
203 CPUExecContext::serialize(ostream
&os
)
205 SERIALIZE_ENUM(_status
);
207 // thread_num and cpu_id are deterministic from the config
208 SERIALIZE_SCALAR(func_exe_inst
);
209 SERIALIZE_SCALAR(inst
);
212 Tick quiesceEndTick
= 0;
213 if (quiesceEvent
->scheduled())
214 quiesceEndTick
= quiesceEvent
->when();
215 SERIALIZE_SCALAR(quiesceEndTick
);
217 kernelStats
->serialize(os
);
223 CPUExecContext::unserialize(Checkpoint
*cp
, const std::string
§ion
)
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
);
233 UNSERIALIZE_SCALAR(quiesceEndTick
);
235 quiesceEvent
->schedule(quiesceEndTick
);
237 kernelStats
->unserialize(cp
, section
);
243 CPUExecContext::activate(int delay
)
245 if (status() == ThreadContext::Active
)
248 lastActivate
= curTick
;
250 if (status() == ThreadContext::Unallocated
) {
251 cpu
->activateWhenReady(thread_num
);
255 _status
= ThreadContext::Active
;
257 // status() == Suspended
258 cpu
->activateContext(thread_num
, delay
);
262 CPUExecContext::suspend()
264 if (status() == ThreadContext::Suspended
)
267 lastActivate
= curTick
;
268 lastSuspend
= curTick
;
271 // Don't change the status from active if there are pending interrupts
272 if (cpu->check_interrupts()) {
273 assert(status() == ThreadContext::Active);
278 _status
= ThreadContext::Suspended
;
279 cpu
->suspendContext(thread_num
);
283 CPUExecContext::deallocate()
285 if (status() == ThreadContext::Unallocated
)
288 _status
= ThreadContext::Unallocated
;
289 cpu
->deallocateContext(thread_num
);
293 CPUExecContext::halt()
295 if (status() == ThreadContext::Halted
)
298 _status
= ThreadContext::Halted
;
299 cpu
->haltContext(thread_num
);
304 CPUExecContext::regStats(const string
&name
)
308 kernelStats
->regStats(name
+ ".kern");
313 CPUExecContext::copyArchRegs(ThreadContext
*src_tc
)
315 TheISA::copyRegs(src_tc
, tc
);
320 CPUExecContext::getVirtPort(ThreadContext
*src_tc
)
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
);
336 CPUExecContext::delVirtPort(VirtualPort
*vp
)
338 // assert(!vp->nullThreadContext());
339 delete vp
->getPeer();