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 "arch/kernel_stats.hh"
43 #include "base/callback.hh"
44 #include "base/cprintf.hh"
45 #include "base/output.hh"
46 #include "base/trace.hh"
47 #include "cpu/profile.hh"
48 #include "cpu/quiesce_event.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 TheISA::ITB
*_itb
, TheISA::DTB
*_dtb
,
64 bool use_kernel_stats
)
65 : ThreadState(_cpu
, -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 TheISA::Kernel::Statistics(system
);
96 SimpleThread::SimpleThread(BaseCPU
*_cpu
, int _thread_num
,
97 Process
*_process
, int _asid
)
98 : ThreadState(_cpu
, -1, _thread_num
, _process
, _asid
),
102 tc
= new ProxyThreadContext
<SimpleThread
>(this);
107 SimpleThread::SimpleThread()
109 : ThreadState(NULL
, -1, -1)
111 : ThreadState(NULL
, -1, -1, NULL
, -1)
114 tc
= new ProxyThreadContext
<SimpleThread
>(this);
118 SimpleThread::~SimpleThread()
128 SimpleThread::takeOverFrom(ThreadContext
*oldContext
)
130 // some things should already be set up
132 assert(system
== oldContext
->getSystemPtr());
134 assert(process
== oldContext
->getProcessPtr());
137 copyState(oldContext
);
139 EndQuiesceEvent
*quiesce
= oldContext
->getQuiesceEvent();
141 // Point the quiesce event's TC at this TC so that it wakes up
146 quiesceEvent
->tc
= tc
;
149 TheISA::Kernel::Statistics
*stats
= oldContext
->getKernelStats();
155 storeCondFailures
= 0;
157 oldContext
->setStatus(ThreadContext::Unallocated
);
161 SimpleThread::copyTC(ThreadContext
*context
)
166 EndQuiesceEvent
*quiesce
= context
->getQuiesceEvent();
168 quiesceEvent
= quiesce
;
170 TheISA::Kernel::Statistics
*stats
= context
->getKernelStats();
178 SimpleThread::copyState(ThreadContext
*oldContext
)
180 // copy over functional state
181 _status
= oldContext
->status();
182 copyArchRegs(oldContext
);
183 cpuId
= oldContext
->readCpuId();
185 funcExeInst
= oldContext
->readFuncExeInst();
187 inst
= oldContext
->getInst();
191 SimpleThread::serialize(ostream
&os
)
193 ThreadState::serialize(os
);
195 // thread_num and cpu_id are deterministic from the config
200 SimpleThread::unserialize(Checkpoint
*cp
, const std::string
§ion
)
202 ThreadState::unserialize(cp
, section
);
203 regs
.unserialize(cp
, section
);
204 // thread_num and cpu_id are deterministic from the config
209 SimpleThread::dumpFuncProfile()
211 std::ostream
*os
= simout
.create(csprintf("profile.%s.dat", cpu
->name()));
212 profile
->dump(tc
, *os
);
217 SimpleThread::activate(int delay
)
219 if (status() == ThreadContext::Active
)
222 lastActivate
= curTick
;
224 if (status() == ThreadContext::Unallocated
) {
225 cpu
->activateWhenReady(tid
);
229 _status
= ThreadContext::Active
;
231 // status() == Suspended
232 cpu
->activateContext(tid
, delay
);
236 SimpleThread::suspend()
238 if (status() == ThreadContext::Suspended
)
241 lastActivate
= curTick
;
242 lastSuspend
= curTick
;
245 // Don't change the status from active if there are pending interrupts
246 if (cpu->check_interrupts()) {
247 assert(status() == ThreadContext::Active);
252 _status
= ThreadContext::Suspended
;
253 cpu
->suspendContext(tid
);
257 SimpleThread::deallocate()
259 if (status() == ThreadContext::Unallocated
)
262 _status
= ThreadContext::Unallocated
;
263 cpu
->deallocateContext(tid
);
269 if (status() == ThreadContext::Halted
)
272 _status
= ThreadContext::Halted
;
273 cpu
->haltContext(tid
);
278 SimpleThread::regStats(const string
&name
)
282 kernelStats
->regStats(name
+ ".kern");
287 SimpleThread::copyArchRegs(ThreadContext
*src_tc
)
289 TheISA::copyRegs(src_tc
, tc
);
294 SimpleThread::getVirtPort(ThreadContext
*src_tc
)
299 VirtualPort
*vp
= new VirtualPort("tc-vport", src_tc
);
300 connectToMemFunc(vp
);
305 SimpleThread::delVirtPort(VirtualPort
*vp
)
307 if (vp
!= virtPort
) {
308 delete vp
->getPeer();