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 "config/the_isa.hh"
38 #include "cpu/base.hh"
39 #include "cpu/simple_thread.hh"
40 #include "cpu/thread_context.hh"
41 #include "params/BaseCPU.hh"
44 #include "arch/kernel_stats.hh"
45 #include "arch/stacktrace.hh"
46 #include "base/callback.hh"
47 #include "base/cprintf.hh"
48 #include "base/output.hh"
49 #include "base/trace.hh"
50 #include "cpu/profile.hh"
51 #include "cpu/quiesce_event.hh"
52 #include "sim/serialize.hh"
53 #include "sim/sim_exit.hh"
55 #include "mem/translating_port.hh"
56 #include "sim/process.hh"
57 #include "sim/system.hh"
64 SimpleThread::SimpleThread(BaseCPU
*_cpu
, int _thread_num
, System
*_sys
,
65 TheISA::TLB
*_itb
, TheISA::TLB
*_dtb
,
66 bool use_kernel_stats
)
67 : ThreadState(_cpu
, _thread_num
),
68 cpu(_cpu
), system(_sys
), itb(_itb
), dtb(_dtb
)
71 tc
= new ProxyThreadContext
<SimpleThread
>(this);
73 quiesceEvent
= new EndQuiesceEvent(tc
);
77 if (cpu
->params()->profile
) {
78 profile
= new FunctionProfile(system
->kernelSymtab
);
80 new MakeCallback
<SimpleThread
,
81 &SimpleThread::dumpFuncProfile
>(this);
82 registerExitCallback(cb
);
85 // let's fill with a dummy node for now so we don't get a segfault
86 // on the first cycle when there's no node available.
87 static ProfileNode dummyNode
;
88 profileNode
= &dummyNode
;
92 kernelStats
= new TheISA::Kernel::Statistics(system
);
95 SimpleThread::SimpleThread(BaseCPU
*_cpu
, int _thread_num
, Process
*_process
,
96 TheISA::TLB
*_itb
, TheISA::TLB
*_dtb
)
97 : ThreadState(_cpu
, _thread_num
, _process
),
98 cpu(_cpu
), itb(_itb
), dtb(_dtb
)
101 tc
= new ProxyThreadContext
<SimpleThread
>(this);
106 SimpleThread::SimpleThread()
108 : ThreadState(NULL
, -1)
110 : ThreadState(NULL
, -1, NULL
)
113 tc
= new ProxyThreadContext
<SimpleThread
>(this);
116 SimpleThread::~SimpleThread()
126 SimpleThread::takeOverFrom(ThreadContext
*oldContext
)
128 // some things should already be set up
130 assert(system
== oldContext
->getSystemPtr());
132 assert(process
== oldContext
->getProcessPtr());
135 copyState(oldContext
);
137 EndQuiesceEvent
*quiesce
= oldContext
->getQuiesceEvent();
139 // Point the quiesce event's TC at this TC so that it wakes up
144 quiesceEvent
->tc
= tc
;
147 TheISA::Kernel::Statistics
*stats
= oldContext
->getKernelStats();
153 storeCondFailures
= 0;
155 oldContext
->setStatus(ThreadContext::Halted
);
159 SimpleThread::copyTC(ThreadContext
*context
)
164 EndQuiesceEvent
*quiesce
= context
->getQuiesceEvent();
166 quiesceEvent
= quiesce
;
168 TheISA::Kernel::Statistics
*stats
= context
->getKernelStats();
176 SimpleThread::copyState(ThreadContext
*oldContext
)
178 // copy over functional state
179 _status
= oldContext
->status();
180 copyArchRegs(oldContext
);
182 funcExeInst
= oldContext
->readFuncExeInst();
184 inst
= oldContext
->getInst();
186 _threadId
= oldContext
->threadId();
187 _contextId
= oldContext
->contextId();
191 SimpleThread::serialize(ostream
&os
)
193 ThreadState::serialize(os
);
194 SERIALIZE_ARRAY(floatRegs
.i
, TheISA::NumFloatRegs
);
195 SERIALIZE_ARRAY(intRegs
, TheISA::NumIntRegs
);
196 SERIALIZE_SCALAR(microPC
);
197 SERIALIZE_SCALAR(nextMicroPC
);
198 SERIALIZE_SCALAR(PC
);
199 SERIALIZE_SCALAR(nextPC
);
200 SERIALIZE_SCALAR(nextNPC
);
201 // thread_num and cpu_id are deterministic from the config
204 // Now must serialize all the ISA dependent state
206 isa
.serialize(cpu
, os
);
211 SimpleThread::unserialize(Checkpoint
*cp
, const std::string
§ion
)
213 ThreadState::unserialize(cp
, section
);
214 UNSERIALIZE_ARRAY(floatRegs
.i
, TheISA::NumFloatRegs
);
215 UNSERIALIZE_ARRAY(intRegs
, TheISA::NumIntRegs
);
216 UNSERIALIZE_SCALAR(microPC
);
217 UNSERIALIZE_SCALAR(nextMicroPC
);
218 UNSERIALIZE_SCALAR(PC
);
219 UNSERIALIZE_SCALAR(nextPC
);
220 UNSERIALIZE_SCALAR(nextNPC
);
221 // thread_num and cpu_id are deterministic from the config
224 // Now must unserialize all the ISA dependent state
226 isa
.unserialize(cpu
, cp
, section
);
231 SimpleThread::dumpFuncProfile()
233 std::ostream
*os
= simout
.create(csprintf("profile.%s.dat", cpu
->name()));
234 profile
->dump(tc
, *os
);
239 SimpleThread::activate(int delay
)
241 if (status() == ThreadContext::Active
)
244 lastActivate
= curTick
;
246 // if (status() == ThreadContext::Unallocated) {
247 // cpu->activateWhenReady(_threadId);
251 _status
= ThreadContext::Active
;
253 // status() == Suspended
254 cpu
->activateContext(_threadId
, delay
);
258 SimpleThread::suspend()
260 if (status() == ThreadContext::Suspended
)
263 lastActivate
= curTick
;
264 lastSuspend
= curTick
;
267 // Don't change the status from active if there are pending interrupts
268 if (cpu->checkInterrupts()) {
269 assert(status() == ThreadContext::Active);
274 _status
= ThreadContext::Suspended
;
275 cpu
->suspendContext(_threadId
);
282 if (status() == ThreadContext::Halted
)
285 _status
= ThreadContext::Halted
;
286 cpu
->haltContext(_threadId
);
291 SimpleThread::regStats(const string
&name
)
295 kernelStats
->regStats(name
+ ".kern");
300 SimpleThread::copyArchRegs(ThreadContext
*src_tc
)
302 TheISA::copyRegs(src_tc
, tc
);