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 "arch/utility.hh"
38 #include "config/the_isa.hh"
39 #include "cpu/base.hh"
40 #include "cpu/simple_thread.hh"
41 #include "cpu/thread_context.hh"
42 #include "params/BaseCPU.hh"
45 #include "arch/kernel_stats.hh"
46 #include "arch/stacktrace.hh"
47 #include "base/callback.hh"
48 #include "base/cprintf.hh"
49 #include "base/output.hh"
50 #include "base/trace.hh"
51 #include "cpu/profile.hh"
52 #include "cpu/quiesce_event.hh"
53 #include "mem/vport.hh"
54 #include "sim/serialize.hh"
55 #include "sim/sim_exit.hh"
57 #include "mem/translating_port.hh"
58 #include "sim/process.hh"
59 #include "sim/system.hh"
66 SimpleThread::SimpleThread(BaseCPU
*_cpu
, int _thread_num
, System
*_sys
,
67 TheISA::TLB
*_itb
, TheISA::TLB
*_dtb
,
68 bool use_kernel_stats
)
69 : ThreadState(_cpu
, _thread_num
),
70 cpu(_cpu
), system(_sys
), itb(_itb
), dtb(_dtb
)
73 tc
= new ProxyThreadContext
<SimpleThread
>(this);
75 quiesceEvent
= new EndQuiesceEvent(tc
);
79 if (cpu
->params()->profile
) {
80 profile
= new FunctionProfile(system
->kernelSymtab
);
82 new MakeCallback
<SimpleThread
,
83 &SimpleThread::dumpFuncProfile
>(this);
84 registerExitCallback(cb
);
87 // let's fill with a dummy node for now so we don't get a segfault
88 // on the first cycle when there's no node available.
89 static ProfileNode dummyNode
;
90 profileNode
= &dummyNode
;
94 kernelStats
= new TheISA::Kernel::Statistics(system
);
97 SimpleThread::SimpleThread(BaseCPU
*_cpu
, int _thread_num
, Process
*_process
,
98 TheISA::TLB
*_itb
, TheISA::TLB
*_dtb
)
99 : ThreadState(_cpu
, _thread_num
, _process
),
100 cpu(_cpu
), itb(_itb
), dtb(_dtb
)
103 tc
= new ProxyThreadContext
<SimpleThread
>(this);
108 SimpleThread::SimpleThread()
110 : ThreadState(NULL
, -1)
112 : ThreadState(NULL
, -1, NULL
)
115 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::Halted
);
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
);
184 funcExeInst
= oldContext
->readFuncExeInst();
187 _threadId
= oldContext
->threadId();
188 _contextId
= oldContext
->contextId();
192 SimpleThread::serialize(ostream
&os
)
194 ThreadState::serialize(os
);
195 SERIALIZE_ARRAY(floatRegs
.i
, TheISA::NumFloatRegs
);
196 SERIALIZE_ARRAY(intRegs
, TheISA::NumIntRegs
);
197 _pcState
.serialize(os
);
198 // thread_num and cpu_id are deterministic from the config
201 // Now must serialize all the ISA dependent state
203 isa
.serialize(cpu
, os
);
208 SimpleThread::unserialize(Checkpoint
*cp
, const std::string
§ion
)
210 ThreadState::unserialize(cp
, section
);
211 UNSERIALIZE_ARRAY(floatRegs
.i
, TheISA::NumFloatRegs
);
212 UNSERIALIZE_ARRAY(intRegs
, TheISA::NumIntRegs
);
213 _pcState
.unserialize(cp
, section
);
214 // thread_num and cpu_id are deterministic from the config
217 // Now must unserialize all the ISA dependent state
219 isa
.unserialize(cpu
, cp
, section
);
224 SimpleThread::dumpFuncProfile()
226 std::ostream
*os
= simout
.create(csprintf("profile.%s.dat", cpu
->name()));
227 profile
->dump(tc
, *os
);
232 SimpleThread::activate(int delay
)
234 if (status() == ThreadContext::Active
)
237 lastActivate
= curTick();
239 // if (status() == ThreadContext::Unallocated) {
240 // cpu->activateWhenReady(_threadId);
244 _status
= ThreadContext::Active
;
246 // status() == Suspended
247 cpu
->activateContext(_threadId
, delay
);
251 SimpleThread::suspend()
253 if (status() == ThreadContext::Suspended
)
256 lastActivate
= curTick();
257 lastSuspend
= curTick();
260 // Don't change the status from active if there are pending interrupts
261 if (cpu->checkInterrupts()) {
262 assert(status() == ThreadContext::Active);
267 _status
= ThreadContext::Suspended
;
268 cpu
->suspendContext(_threadId
);
275 if (status() == ThreadContext::Halted
)
278 _status
= ThreadContext::Halted
;
279 cpu
->haltContext(_threadId
);
284 SimpleThread::regStats(const string
&name
)
288 kernelStats
->regStats(name
+ ".kern");
293 SimpleThread::copyArchRegs(ThreadContext
*src_tc
)
295 TheISA::copyRegs(src_tc
, tc
);