simple_thread.cc revision 9428
1/*
2 * Copyright (c) 2001-2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Steve Reinhardt
29 *          Nathan Binkert
30 *          Lisa Hsu
31 *          Kevin Lim
32 */
33
34#include <string>
35
36#include "arch/isa_traits.hh"
37#include "arch/kernel_stats.hh"
38#include "arch/stacktrace.hh"
39#include "arch/utility.hh"
40#include "base/callback.hh"
41#include "base/cprintf.hh"
42#include "base/output.hh"
43#include "base/trace.hh"
44#include "config/the_isa.hh"
45#include "cpu/base.hh"
46#include "cpu/profile.hh"
47#include "cpu/quiesce_event.hh"
48#include "cpu/simple_thread.hh"
49#include "cpu/thread_context.hh"
50#include "mem/fs_translating_port_proxy.hh"
51#include "mem/se_translating_port_proxy.hh"
52#include "params/BaseCPU.hh"
53#include "sim/full_system.hh"
54#include "sim/process.hh"
55#include "sim/serialize.hh"
56#include "sim/sim_exit.hh"
57#include "sim/system.hh"
58
59using namespace std;
60
61// constructor
62SimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, System *_sys,
63                           Process *_process, TheISA::TLB *_itb,
64                           TheISA::TLB *_dtb, TheISA::ISA *_isa)
65    : ThreadState(_cpu, _thread_num, _process), isa(_isa), system(_sys),
66      itb(_itb), dtb(_dtb)
67{
68    clearArchRegs();
69    tc = new ProxyThreadContext<SimpleThread>(this);
70}
71
72SimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, System *_sys,
73                           TheISA::TLB *_itb, TheISA::TLB *_dtb,
74                           TheISA::ISA *_isa, bool use_kernel_stats)
75    : ThreadState(_cpu, _thread_num, NULL), isa(_isa), system(_sys), itb(_itb),
76      dtb(_dtb)
77{
78    tc = new ProxyThreadContext<SimpleThread>(this);
79
80    quiesceEvent = new EndQuiesceEvent(tc);
81
82    clearArchRegs();
83
84    if (baseCpu->params()->profile) {
85        profile = new FunctionProfile(system->kernelSymtab);
86        Callback *cb =
87            new MakeCallback<SimpleThread,
88            &SimpleThread::dumpFuncProfile>(this);
89        registerExitCallback(cb);
90    }
91
92    // let's fill with a dummy node for now so we don't get a segfault
93    // on the first cycle when there's no node available.
94    static ProfileNode dummyNode;
95    profileNode = &dummyNode;
96    profilePC = 3;
97
98    if (use_kernel_stats)
99        kernelStats = new TheISA::Kernel::Statistics(system);
100}
101
102SimpleThread::~SimpleThread()
103{
104    delete tc;
105}
106
107void
108SimpleThread::takeOverFrom(ThreadContext *oldContext)
109{
110    // some things should already be set up
111    if (FullSystem)
112        assert(system == oldContext->getSystemPtr());
113    assert(process == oldContext->getProcessPtr());
114
115    copyState(oldContext);
116    if (FullSystem) {
117        EndQuiesceEvent *quiesce = oldContext->getQuiesceEvent();
118        if (quiesce) {
119            // Point the quiesce event's TC at this TC so that it wakes up
120            // the proper CPU.
121            quiesce->tc = tc;
122        }
123        if (quiesceEvent) {
124            quiesceEvent->tc = tc;
125        }
126
127        TheISA::Kernel::Statistics *stats = oldContext->getKernelStats();
128        if (stats) {
129            kernelStats = stats;
130        }
131    }
132
133    storeCondFailures = 0;
134
135    oldContext->setStatus(ThreadContext::Halted);
136}
137
138void
139SimpleThread::copyTC(ThreadContext *context)
140{
141    copyState(context);
142
143    if (FullSystem) {
144        EndQuiesceEvent *quiesce = context->getQuiesceEvent();
145        if (quiesce) {
146            quiesceEvent = quiesce;
147        }
148        TheISA::Kernel::Statistics *stats = context->getKernelStats();
149        if (stats) {
150            kernelStats = stats;
151        }
152    }
153}
154
155void
156SimpleThread::copyState(ThreadContext *oldContext)
157{
158    // copy over functional state
159    _status = oldContext->status();
160    copyArchRegs(oldContext);
161    if (FullSystem)
162        funcExeInst = oldContext->readFuncExeInst();
163
164    _threadId = oldContext->threadId();
165    _contextId = oldContext->contextId();
166}
167
168void
169SimpleThread::serialize(ostream &os)
170{
171    ThreadState::serialize(os);
172    ::serialize(*tc, os);
173}
174
175
176void
177SimpleThread::unserialize(Checkpoint *cp, const std::string &section)
178{
179    ThreadState::unserialize(cp, section);
180    ::unserialize(*tc, cp, section);
181}
182
183void
184SimpleThread::dumpFuncProfile()
185{
186    std::ostream *os = simout.create(csprintf("profile.%s.dat",
187                                              baseCpu->name()));
188    profile->dump(tc, *os);
189}
190
191void
192SimpleThread::activate(Cycles delay)
193{
194    if (status() == ThreadContext::Active)
195        return;
196
197    lastActivate = curTick();
198
199//    if (status() == ThreadContext::Unallocated) {
200//      cpu->activateWhenReady(_threadId);
201//      return;
202//   }
203
204    _status = ThreadContext::Active;
205
206    // status() == Suspended
207    baseCpu->activateContext(_threadId, delay);
208}
209
210void
211SimpleThread::suspend()
212{
213    if (status() == ThreadContext::Suspended)
214        return;
215
216    lastActivate = curTick();
217    lastSuspend = curTick();
218    _status = ThreadContext::Suspended;
219    baseCpu->suspendContext(_threadId);
220}
221
222
223void
224SimpleThread::halt()
225{
226    if (status() == ThreadContext::Halted)
227        return;
228
229    _status = ThreadContext::Halted;
230    baseCpu->haltContext(_threadId);
231}
232
233
234void
235SimpleThread::regStats(const string &name)
236{
237    if (FullSystem && kernelStats)
238        kernelStats->regStats(name + ".kern");
239}
240
241void
242SimpleThread::copyArchRegs(ThreadContext *src_tc)
243{
244    TheISA::copyRegs(src_tc, tc);
245}
246
247