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 "base/callback.hh"
43 #include "base/cprintf.hh"
44 #include "base/output.hh"
45 #include "base/trace.hh"
46 #include "cpu/profile.hh"
47 #include "cpu/quiesce_event.hh"
48 #include "kern/kernel_stats.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 AlphaITB
*_itb
, AlphaDTB
*_dtb
,
64 bool use_kernel_stats
)
65 : ThreadState(-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 Kernel::Statistics(system
);
95 physPort
= new FunctionalPort(csprintf("%s-%d-funcport",
97 mem_port
= system
->physmem
->getPort("functional");
98 mem_port
->setPeer(physPort
);
99 physPort
->setPeer(mem_port
);
101 virtPort
= new VirtualPort(csprintf("%s-%d-vport",
103 mem_port
= system
->physmem
->getPort("functional");
104 mem_port
->setPeer(virtPort
);
105 virtPort
->setPeer(mem_port
);
108 SimpleThread::SimpleThread(BaseCPU
*_cpu
, int _thread_num
,
109 Process
*_process
, int _asid
, MemObject
* memobj
)
110 : ThreadState(-1, _thread_num
, _process
, _asid
, memobj
),
113 /* Use this port to for syscall emulation writes to memory. */
115 port
= new TranslatingPort(csprintf("%s-%d-funcport",
117 process
->pTable
, false);
118 mem_port
= memobj
->getPort("functional");
119 mem_port
->setPeer(port
);
120 port
->setPeer(mem_port
);
123 tc
= new ProxyThreadContext
<SimpleThread
>(this);
128 SimpleThread::SimpleThread()
130 : ThreadState(-1, -1)
132 : ThreadState(-1, -1, NULL
, -1, NULL
)
135 tc
= new ProxyThreadContext
<SimpleThread
>(this);
139 SimpleThread::~SimpleThread()
145 SimpleThread::takeOverFrom(ThreadContext
*oldContext
)
147 // some things should already be set up
149 assert(system
== oldContext
->getSystemPtr());
151 assert(process
== oldContext
->getProcessPtr());
154 copyState(oldContext
);
156 EndQuiesceEvent
*quiesce
= oldContext
->getQuiesceEvent();
158 // Point the quiesce event's TC at this TC so that it wakes up
163 quiesceEvent
->tc
= tc
;
167 storeCondFailures
= 0;
169 oldContext
->setStatus(ThreadContext::Unallocated
);
173 SimpleThread::copyTC(ThreadContext
*context
)
178 EndQuiesceEvent
*quiesce
= context
->getQuiesceEvent();
180 quiesceEvent
= quiesce
;
182 Kernel::Statistics
*stats
= context
->getKernelStats();
190 SimpleThread::copyState(ThreadContext
*oldContext
)
192 // copy over functional state
193 _status
= oldContext
->status();
194 copyArchRegs(oldContext
);
195 cpuId
= oldContext
->readCpuId();
197 funcExeInst
= oldContext
->readFuncExeInst();
199 inst
= oldContext
->getInst();
203 SimpleThread::serialize(ostream
&os
)
205 ThreadState::serialize(os
);
207 // thread_num and cpu_id are deterministic from the config
212 SimpleThread::unserialize(Checkpoint
*cp
, const std::string
§ion
)
214 ThreadState::unserialize(cp
, section
);
215 regs
.unserialize(cp
, section
);
216 // thread_num and cpu_id are deterministic from the config
221 SimpleThread::dumpFuncProfile()
223 std::ostream
*os
= simout
.create(csprintf("profile.%s.dat", cpu
->name()));
224 profile
->dump(tc
, *os
);
229 SimpleThread::activate(int delay
)
231 if (status() == ThreadContext::Active
)
234 lastActivate
= curTick
;
236 if (status() == ThreadContext::Unallocated
) {
237 cpu
->activateWhenReady(tid
);
241 _status
= ThreadContext::Active
;
243 // status() == Suspended
244 cpu
->activateContext(tid
, delay
);
248 SimpleThread::suspend()
250 if (status() == ThreadContext::Suspended
)
253 lastActivate
= curTick
;
254 lastSuspend
= curTick
;
257 // Don't change the status from active if there are pending interrupts
258 if (cpu->check_interrupts()) {
259 assert(status() == ThreadContext::Active);
264 _status
= ThreadContext::Suspended
;
265 cpu
->suspendContext(tid
);
269 SimpleThread::deallocate()
271 if (status() == ThreadContext::Unallocated
)
274 _status
= ThreadContext::Unallocated
;
275 cpu
->deallocateContext(tid
);
281 if (status() == ThreadContext::Halted
)
284 _status
= ThreadContext::Halted
;
285 cpu
->haltContext(tid
);
290 SimpleThread::regStats(const string
&name
)
294 kernelStats
->regStats(name
+ ".kern");
299 SimpleThread::copyArchRegs(ThreadContext
*src_tc
)
301 TheISA::copyRegs(src_tc
, tc
);
306 SimpleThread::getVirtPort(ThreadContext
*src_tc
)
314 vp
= new VirtualPort("tc-vport", src_tc
);
315 mem_port
= system
->physmem
->getPort("functional");
316 mem_port
->setPeer(vp
);
317 vp
->setPeer(mem_port
);
322 SimpleThread::delVirtPort(VirtualPort
*vp
)
324 if (vp
!= virtPort
) {
325 delete vp
->getPeer();