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"
40 #include "params/BaseCPU.hh"
43 #include "arch/kernel_stats.hh"
44 #include "arch/stacktrace.hh"
45 #include "base/callback.hh"
46 #include "base/cprintf.hh"
47 #include "base/output.hh"
48 #include "base/trace.hh"
49 #include "cpu/profile.hh"
50 #include "cpu/quiesce_event.hh"
51 #include "sim/serialize.hh"
52 #include "sim/sim_exit.hh"
54 #include "mem/translating_port.hh"
55 #include "sim/process.hh"
56 #include "sim/system.hh"
63 SimpleThread::SimpleThread(BaseCPU
*_cpu
, int _thread_num
, System
*_sys
,
64 TheISA::TLB
*_itb
, TheISA::TLB
*_dtb
,
65 bool use_kernel_stats
)
66 : ThreadState(_cpu
, _thread_num
),
67 cpu(_cpu
), system(_sys
), itb(_itb
), dtb(_dtb
)
70 tc
= new ProxyThreadContext
<SimpleThread
>(this);
72 quiesceEvent
= new EndQuiesceEvent(tc
);
76 if (cpu
->params()->profile
) {
77 profile
= new FunctionProfile(system
->kernelSymtab
);
79 new MakeCallback
<SimpleThread
,
80 &SimpleThread::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 kernelStats
= new TheISA::Kernel::Statistics(system
);
94 SimpleThread::SimpleThread(BaseCPU
*_cpu
, int _thread_num
, Process
*_process
,
95 TheISA::TLB
*_itb
, TheISA::TLB
*_dtb
)
96 : ThreadState(_cpu
, _thread_num
, _process
),
97 cpu(_cpu
), itb(_itb
), dtb(_dtb
)
100 tc
= new ProxyThreadContext
<SimpleThread
>(this);
105 SimpleThread::SimpleThread()
107 : ThreadState(NULL
, -1)
109 : ThreadState(NULL
, -1, NULL
)
112 tc
= new ProxyThreadContext
<SimpleThread
>(this);
115 SimpleThread::~SimpleThread()
125 SimpleThread::takeOverFrom(ThreadContext
*oldContext
)
127 // some things should already be set up
129 assert(system
== oldContext
->getSystemPtr());
131 assert(process
== oldContext
->getProcessPtr());
134 copyState(oldContext
);
136 EndQuiesceEvent
*quiesce
= oldContext
->getQuiesceEvent();
138 // Point the quiesce event's TC at this TC so that it wakes up
143 quiesceEvent
->tc
= tc
;
146 TheISA::Kernel::Statistics
*stats
= oldContext
->getKernelStats();
152 storeCondFailures
= 0;
154 oldContext
->setStatus(ThreadContext::Halted
);
158 SimpleThread::copyTC(ThreadContext
*context
)
163 EndQuiesceEvent
*quiesce
= context
->getQuiesceEvent();
165 quiesceEvent
= quiesce
;
167 TheISA::Kernel::Statistics
*stats
= context
->getKernelStats();
175 SimpleThread::copyState(ThreadContext
*oldContext
)
177 // copy over functional state
178 _status
= oldContext
->status();
179 copyArchRegs(oldContext
);
181 funcExeInst
= oldContext
->readFuncExeInst();
183 inst
= oldContext
->getInst();
185 _threadId
= oldContext
->threadId();
186 _contextId
= oldContext
->contextId();
190 SimpleThread::serialize(ostream
&os
)
192 ThreadState::serialize(os
);
193 SERIALIZE_ARRAY(floatRegs
.i
, TheISA::NumFloatRegs
);
194 SERIALIZE_ARRAY(intRegs
, TheISA::NumIntRegs
);
195 SERIALIZE_SCALAR(microPC
);
196 SERIALIZE_SCALAR(nextMicroPC
);
197 SERIALIZE_SCALAR(PC
);
198 SERIALIZE_SCALAR(nextPC
);
199 SERIALIZE_SCALAR(nextNPC
);
200 // thread_num and cpu_id are deterministic from the config
205 SimpleThread::unserialize(Checkpoint
*cp
, const std::string
§ion
)
207 ThreadState::unserialize(cp
, section
);
208 UNSERIALIZE_ARRAY(floatRegs
.i
, TheISA::NumFloatRegs
);
209 UNSERIALIZE_ARRAY(intRegs
, TheISA::NumIntRegs
);
210 UNSERIALIZE_SCALAR(microPC
);
211 UNSERIALIZE_SCALAR(nextMicroPC
);
212 UNSERIALIZE_SCALAR(PC
);
213 UNSERIALIZE_SCALAR(nextPC
);
214 UNSERIALIZE_SCALAR(nextNPC
);
215 // thread_num and cpu_id are deterministic from the config
220 SimpleThread::dumpFuncProfile()
222 std::ostream
*os
= simout
.create(csprintf("profile.%s.dat", cpu
->name()));
223 profile
->dump(tc
, *os
);
228 SimpleThread::activate(int delay
)
230 if (status() == ThreadContext::Active
)
233 lastActivate
= curTick
;
235 // if (status() == ThreadContext::Unallocated) {
236 // cpu->activateWhenReady(_threadId);
240 _status
= ThreadContext::Active
;
242 // status() == Suspended
243 cpu
->activateContext(_threadId
, delay
);
247 SimpleThread::suspend()
249 if (status() == ThreadContext::Suspended
)
252 lastActivate
= curTick
;
253 lastSuspend
= curTick
;
256 // Don't change the status from active if there are pending interrupts
257 if (cpu->checkInterrupts()) {
258 assert(status() == ThreadContext::Active);
263 _status
= ThreadContext::Suspended
;
264 cpu
->suspendContext(_threadId
);
271 if (status() == ThreadContext::Halted
)
274 _status
= ThreadContext::Halted
;
275 cpu
->haltContext(_threadId
);
280 SimpleThread::regStats(const string
&name
)
284 kernelStats
->regStats(name
+ ".kern");
289 SimpleThread::copyArchRegs(ThreadContext
*src_tc
)
291 TheISA::copyRegs(src_tc
, tc
);