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::ITB
*_itb
, TheISA::DTB
*_dtb
,
65 bool use_kernel_stats
)
66 : ThreadState(_cpu
, _thread_num
), cpu(_cpu
), system(_sys
), itb(_itb
),
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::ITB
*_itb
, TheISA::DTB
*_dtb
, int _asid
)
96 : ThreadState(_cpu
, _thread_num
, _process
, _asid
),
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
, -1)
112 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::Unallocated
);
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 regs
.serialize(cpu
, 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(cpu
, 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(_threadId);
229 _status
= ThreadContext::Active
;
231 // status() == Suspended
232 cpu
->activateContext(_threadId
, 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->checkInterrupts()) {
247 assert(status() == ThreadContext::Active);
252 _status
= ThreadContext::Suspended
;
253 cpu
->suspendContext(_threadId
);
257 SimpleThread::deallocate()
259 if (status() == ThreadContext::Unallocated
)
262 _status
= ThreadContext::Unallocated
;
263 cpu
->deallocateContext(_threadId
);
269 if (status() == ThreadContext::Halted
)
272 _status
= ThreadContext::Halted
;
273 cpu
->haltContext(_threadId
);
278 SimpleThread::regStats(const string
&name
)
282 kernelStats
->regStats(name
+ ".kern");
287 SimpleThread::copyArchRegs(ThreadContext
*src_tc
)
289 TheISA::copyRegs(src_tc
, tc
);