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/simple_thread.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 SimpleThread::SimpleThread(BaseCPU
*_cpu
, int _thread_num
, System
*_sys
,
63 AlphaITB
*_itb
, AlphaDTB
*_dtb
,
64 bool use_kernel_stats
)
65 : ThreadState(-1, _thread_num
), cpu(_cpu
), system(_sys
), itb(_itb
),
69 tc
= new ProxyThreadContext
<SimpleThread
>(this);
71 quiesceEvent
= new EndQuiesceEvent(tc
);
75 if (cpu
->params
->profile
) {
76 profile
= new FunctionProfile(system
->kernelSymtab
);
78 new MakeCallback
<SimpleThread
,
79 &SimpleThread::dumpFuncProfile
>(this);
80 registerExitCallback(cb
);
83 // let's fill with a dummy node for now so we don't get a segfault
84 // on the first cycle when there's no node available.
85 static ProfileNode dummyNode
;
86 profileNode
= &dummyNode
;
89 if (use_kernel_stats
) {
90 kernelStats
= new Kernel::Statistics(system
);
95 physPort
= new FunctionalPort(csprintf("%s-%d-funcport",
97 mem_port
= system
->physmem
->getPort("functional");
98 mem_port
->setPeer(physPort
);
99 physPort
->setPeer(mem_port
);
101 virtPort
= new VirtualPort(csprintf("%s-%d-vport",
103 mem_port
= system
->physmem
->getPort("functional");
104 mem_port
->setPeer(virtPort
);
105 virtPort
->setPeer(mem_port
);
108 SimpleThread::SimpleThread(BaseCPU
*_cpu
, int _thread_num
,
109 Process
*_process
, int _asid
, MemObject
* memobj
)
110 : ThreadState(-1, _thread_num
, memobj
, _process
, _asid
),
113 /* Use this port to for syscall emulation writes to memory. */
115 port
= new TranslatingPort(csprintf("%s-%d-funcport",
117 process
->pTable
, false);
118 mem_port
= memobj
->getPort("functional");
119 mem_port
->setPeer(port
);
120 port
->setPeer(mem_port
);
123 tc
= new ProxyThreadContext
<SimpleThread
>(this);
126 SimpleThread::SimpleThread(RegFile
*regFile
)
127 : ThreadState(-1, -1, NULL
, NULL
, -1), cpu(NULL
)
130 tc
= new ProxyThreadContext
<SimpleThread
>(this);
135 SimpleThread::~SimpleThread()
141 SimpleThread::takeOverFrom(ThreadContext
*oldContext
)
143 // some things should already be set up
145 assert(system
== oldContext
->getSystemPtr());
147 assert(process
== oldContext
->getProcessPtr());
150 // copy over functional state
151 _status
= oldContext
->status();
152 copyArchRegs(oldContext
);
153 cpuId
= oldContext
->readCpuId();
155 funcExeInst
= oldContext
->readFuncExeInst();
157 EndQuiesceEvent
*quiesce
= oldContext
->getQuiesceEvent();
159 // Point the quiesce event's TC at this TC so that it wakes up
164 quiesceEvent
->tc
= tc
;
168 storeCondFailures
= 0;
170 oldContext
->setStatus(ThreadContext::Unallocated
);
174 SimpleThread::serialize(ostream
&os
)
176 SERIALIZE_ENUM(_status
);
178 // thread_num and cpu_id are deterministic from the config
179 SERIALIZE_SCALAR(funcExeInst
);
180 SERIALIZE_SCALAR(inst
);
183 Tick quiesceEndTick
= 0;
184 if (quiesceEvent
->scheduled())
185 quiesceEndTick
= quiesceEvent
->when();
186 SERIALIZE_SCALAR(quiesceEndTick
);
188 kernelStats
->serialize(os
);
194 SimpleThread::unserialize(Checkpoint
*cp
, const std::string
§ion
)
196 UNSERIALIZE_ENUM(_status
);
197 regs
.unserialize(cp
, section
);
198 // thread_num and cpu_id are deterministic from the config
199 UNSERIALIZE_SCALAR(funcExeInst
);
200 UNSERIALIZE_SCALAR(inst
);
204 UNSERIALIZE_SCALAR(quiesceEndTick
);
206 quiesceEvent
->schedule(quiesceEndTick
);
208 kernelStats
->unserialize(cp
, section
);
214 SimpleThread::dumpFuncProfile()
216 std::ostream
*os
= simout
.create(csprintf("profile.%s.dat", cpu
->name()));
217 profile
->dump(tc
, *os
);
222 SimpleThread::activate(int delay
)
224 if (status() == ThreadContext::Active
)
227 lastActivate
= curTick
;
229 if (status() == ThreadContext::Unallocated
) {
230 cpu
->activateWhenReady(tid
);
234 _status
= ThreadContext::Active
;
236 // status() == Suspended
237 cpu
->activateContext(tid
, delay
);
241 SimpleThread::suspend()
243 if (status() == ThreadContext::Suspended
)
246 lastActivate
= curTick
;
247 lastSuspend
= curTick
;
250 // Don't change the status from active if there are pending interrupts
251 if (cpu->check_interrupts()) {
252 assert(status() == ThreadContext::Active);
257 _status
= ThreadContext::Suspended
;
258 cpu
->suspendContext(tid
);
262 SimpleThread::deallocate()
264 if (status() == ThreadContext::Unallocated
)
267 _status
= ThreadContext::Unallocated
;
268 cpu
->deallocateContext(tid
);
274 if (status() == ThreadContext::Halted
)
277 _status
= ThreadContext::Halted
;
278 cpu
->haltContext(tid
);
283 SimpleThread::regStats(const string
&name
)
287 kernelStats
->regStats(name
+ ".kern");
292 SimpleThread::copyArchRegs(ThreadContext
*src_tc
)
294 TheISA::copyRegs(src_tc
, tc
);
299 SimpleThread::getVirtPort(ThreadContext
*src_tc
)
307 vp
= new VirtualPort("tc-vport", src_tc
);
308 mem_port
= system
->physmem
->getPort("functional");
309 mem_port
->setPeer(vp
);
310 vp
->setPeer(mem_port
);
315 SimpleThread::delVirtPort(VirtualPort
*vp
)
317 if (vp
!= virtPort
) {
318 delete vp
->getPeer();