simple_thread.cc revision 2864:eab7ff8f6d72
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 "cpu/base.hh"
38#include "cpu/simple_thread.hh"
39#include "cpu/thread_context.hh"
40
41#if FULL_SYSTEM
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"
52#else
53#include "sim/process.hh"
54#include "sim/system.hh"
55#include "mem/translating_port.hh"
56#endif
57
58using namespace std;
59
60// constructor
61#if FULL_SYSTEM
62SimpleThread::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),
66      dtb(_dtb)
67
68{
69    tc = new ProxyThreadContext<SimpleThread>(this);
70
71    quiesceEvent = new EndQuiesceEvent(tc);
72
73    regs.clear();
74
75    if (cpu->params->profile) {
76        profile = new FunctionProfile(system->kernelSymtab);
77        Callback *cb =
78            new MakeCallback<SimpleThread,
79            &SimpleThread::dumpFuncProfile>(this);
80        registerExitCallback(cb);
81    }
82
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;
87    profilePC = 3;
88
89    if (use_kernel_stats) {
90        kernelStats = new Kernel::Statistics(system);
91    } else {
92        kernelStats = NULL;
93    }
94    Port *mem_port;
95    physPort = new FunctionalPort(csprintf("%s-%d-funcport",
96                                           cpu->name(), tid));
97    mem_port = system->physmem->getPort("functional");
98    mem_port->setPeer(physPort);
99    physPort->setPeer(mem_port);
100
101    virtPort = new VirtualPort(csprintf("%s-%d-vport",
102                                        cpu->name(), tid));
103    mem_port = system->physmem->getPort("functional");
104    mem_port->setPeer(virtPort);
105    virtPort->setPeer(mem_port);
106}
107#else
108SimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num,
109                         Process *_process, int _asid, MemObject* memobj)
110    : ThreadState(-1, _thread_num, _process, _asid, memobj),
111      cpu(_cpu)
112{
113    /* Use this port to for syscall emulation writes to memory. */
114    Port *mem_port;
115    port = new TranslatingPort(csprintf("%s-%d-funcport",
116                                        cpu->name(), tid),
117                               process->pTable, false);
118    mem_port = memobj->getPort("functional");
119    mem_port->setPeer(port);
120    port->setPeer(mem_port);
121
122    regs.clear();
123    tc = new ProxyThreadContext<SimpleThread>(this);
124}
125
126#endif
127
128SimpleThread::SimpleThread()
129#if FULL_SYSTEM
130    : ThreadState(-1, -1)
131#else
132    : ThreadState(-1, -1, NULL, -1, NULL)
133#endif
134{
135    tc = new ProxyThreadContext<SimpleThread>(this);
136    regs.clear();
137}
138
139SimpleThread::~SimpleThread()
140{
141    delete tc;
142}
143
144void
145SimpleThread::takeOverFrom(ThreadContext *oldContext)
146{
147    // some things should already be set up
148#if FULL_SYSTEM
149    assert(system == oldContext->getSystemPtr());
150#else
151    assert(process == oldContext->getProcessPtr());
152#endif
153
154    copyState(oldContext);
155#if FULL_SYSTEM
156    EndQuiesceEvent *quiesce = oldContext->getQuiesceEvent();
157    if (quiesce) {
158        // Point the quiesce event's TC at this TC so that it wakes up
159        // the proper CPU.
160        quiesce->tc = tc;
161    }
162    if (quiesceEvent) {
163        quiesceEvent->tc = tc;
164    }
165#endif
166
167    storeCondFailures = 0;
168
169    oldContext->setStatus(ThreadContext::Unallocated);
170}
171
172void
173SimpleThread::copyTC(ThreadContext *context)
174{
175    copyState(context);
176
177#if FULL_SYSTEM
178    EndQuiesceEvent *quiesce = context->getQuiesceEvent();
179    if (quiesce) {
180        quiesceEvent = quiesce;
181    }
182    Kernel::Statistics *stats = context->getKernelStats();
183    if (stats) {
184        kernelStats = stats;
185    }
186#endif
187}
188
189void
190SimpleThread::copyState(ThreadContext *oldContext)
191{
192    // copy over functional state
193    _status = oldContext->status();
194    copyArchRegs(oldContext);
195    cpuId = oldContext->readCpuId();
196#if !FULL_SYSTEM
197    funcExeInst = oldContext->readFuncExeInst();
198#endif
199}
200
201void
202SimpleThread::serialize(ostream &os)
203{
204    ThreadState::serialize(os);
205    regs.serialize(os);
206    // thread_num and cpu_id are deterministic from the config
207}
208
209
210void
211SimpleThread::unserialize(Checkpoint *cp, const std::string &section)
212{
213    ThreadState::unserialize(cp, section);
214    regs.unserialize(cp, section);
215    // thread_num and cpu_id are deterministic from the config
216}
217
218#if FULL_SYSTEM
219void
220SimpleThread::dumpFuncProfile()
221{
222    std::ostream *os = simout.create(csprintf("profile.%s.dat", cpu->name()));
223    profile->dump(tc, *os);
224}
225#endif
226
227void
228SimpleThread::activate(int delay)
229{
230    if (status() == ThreadContext::Active)
231        return;
232
233    lastActivate = curTick;
234
235    if (status() == ThreadContext::Unallocated) {
236        cpu->activateWhenReady(tid);
237        return;
238    }
239
240    _status = ThreadContext::Active;
241
242    // status() == Suspended
243    cpu->activateContext(tid, delay);
244}
245
246void
247SimpleThread::suspend()
248{
249    if (status() == ThreadContext::Suspended)
250        return;
251
252    lastActivate = curTick;
253    lastSuspend = curTick;
254/*
255#if FULL_SYSTEM
256    // Don't change the status from active if there are pending interrupts
257    if (cpu->check_interrupts()) {
258        assert(status() == ThreadContext::Active);
259        return;
260    }
261#endif
262*/
263    _status = ThreadContext::Suspended;
264    cpu->suspendContext(tid);
265}
266
267void
268SimpleThread::deallocate()
269{
270    if (status() == ThreadContext::Unallocated)
271        return;
272
273    _status = ThreadContext::Unallocated;
274    cpu->deallocateContext(tid);
275}
276
277void
278SimpleThread::halt()
279{
280    if (status() == ThreadContext::Halted)
281        return;
282
283    _status = ThreadContext::Halted;
284    cpu->haltContext(tid);
285}
286
287
288void
289SimpleThread::regStats(const string &name)
290{
291#if FULL_SYSTEM
292    if (kernelStats)
293        kernelStats->regStats(name + ".kern");
294#endif
295}
296
297void
298SimpleThread::copyArchRegs(ThreadContext *src_tc)
299{
300    TheISA::copyRegs(src_tc, tc);
301}
302
303#if FULL_SYSTEM
304VirtualPort*
305SimpleThread::getVirtPort(ThreadContext *src_tc)
306{
307    if (!src_tc)
308        return virtPort;
309
310    VirtualPort *vp;
311    Port *mem_port;
312
313    vp = new VirtualPort("tc-vport", src_tc);
314    mem_port = system->physmem->getPort("functional");
315    mem_port->setPeer(vp);
316    vp->setPeer(mem_port);
317    return vp;
318}
319
320void
321SimpleThread::delVirtPort(VirtualPort *vp)
322{
323    if (vp != virtPort) {
324        delete vp->getPeer();
325        delete vp;
326    }
327}
328
329
330#endif
331
332